Oliver Drotbohm opened SPR-7169 and commented

It would be cool if test classes executed with SpringJUnit4ClassRunner would be considered as @Configuration classes, too. This way the following would be possible.

@RunWith(SpringJUnitClassRunner.class)
@ContextConfigLocation("classpath:foo.xml")
public class SampleTest {

  @Autowired
  private MyService myService;

  @Bean
  public MyRepository repository() {
    return Mockito.mock(MyRepository.class);
  }

  // Testcases
}

IMHO this has the benefit of not polluting the test resources with zillions of config files that you might want to declare to override certain beans from your default configuration (which you would additionally load inside @ContextConfigLocation). As this co-locates customization needed for the tests and the tests themselves it reduces scattering of artifacts needed for the tests.


Affects: 3.0.2

3 votes, 7 watchers

Comment From: spring-projects-issues

Chris Beams commented

Interesting idea, Ollie!

Comment From: loganmzz

Why not use @TestConfiguration ?

Comment From: sbrannen

Why not use @TestConfiguration?

This issue was raised in 2010 for the core Spring Framework; whereas, @TestConfiguration is a feature specific to Spring Boot.

Comment From: loganmzz

Good points, I didn't catch it !

Comment From: sbrannen

  • superseded by #29917