Skip to content

Spring Test should not modify the application context under test [SPR-8503] #13149

Closed

Description

@spring-projects-issues

Neale Upstone opened SPR-8503 and commented

When using @ContextConfiguration in a test @Autowired annotated fields get wired, even if context:annotation-driven/ has not been specified.

This is incorrect behaviour, as it means that a test can pass against a context configuration which when used in production will not behave that way.

A better approach would be to load the specified configuration into one application context, and then use a child context for wiring the test fixture.

The child context can then have the additional post processors, without polluting the application context under test.


Affects: 3.1 M2

Issue Links:

Activity

  1. spring-projects-issues commented on Jul 8, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Neale Upstone commented

    As a workaround, here's my recommendation for what to do for your application contexts in Spring 3.0 and earlier.

    
    public class RepositoryContextLoadTest {
    	
    	private static final String[] locations = {"classpath:/fuzzy-repository-context.xml"};
    
    	@Test 
    	public void matchesForAPersonAreInOrder(){
    		GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    		context.load(locations);
    		context.refresh();
    		context.start();
    		
    		FuzzyRepository<?> repo = context.getBean(FuzzyRepository.class);
    		assertNotNull(repo);
    		
    		context.close();
    	}
    }
    
  2. spring-projects-issues commented on Jul 8, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Neale Upstone commented

    (If only I could edit my own comment: the name of the test method shouldn't be my quick copy pasted one ;) )

  3. spring-projects-issues commented on Jul 17, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Duplicate of #9309.

  4. added
    in: test
    type: enhancement
    status: duplicate
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: teststatus: duplicatetype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions