Spring Framework
  1. Spring Framework
  2. SPR-8503

Spring Test should not modify the application context under test

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Duplicate
    • Affects Version/s: 3.1 M2
    • Fix Version/s: None
    • Component/s: Test
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      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.

        Issue Links

          Activity

          Hide
          Neale Upstone added a comment -

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

          RepositoryContextLoadTest.java
          
          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();
          	}
          }
          
          Show
          Neale Upstone added a comment - As a workaround, here's my recommendation for what to do for your application contexts in Spring 3.0 and earlier. RepositoryContextLoadTest.java 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(); } }
          Hide
          Neale Upstone added a comment -

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

          Show
          Neale Upstone added a comment - (If only I could edit my own comment: the name of the test method shouldn't be my quick copy pasted one )
          Hide
          Sam Brannen added a comment -

          Duplicate of SPR-4632.

          Show
          Sam Brannen added a comment - Duplicate of SPR-4632 .

            People

            • Assignee:
              Sam Brannen
              Reporter:
              Neale Upstone
              Last updater:
              Trevor Marshall
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                2 years, 40 weeks, 3 days ago