I have some integration tests that are supposed to mock out one of many beans in my system. To do this, I have a @Configuration
that looks like this:
@Configuration
public class MockContext {
@Primary
@Bean
public RealBean realBean() {
return new MockBean();
}
}
I noticed that this method gets used if RealBean
is a java class without @Component
. But if RealBean
is a @Component
, I have to change this context method to look like this:
@Configuration
public class MockContext {
@Primary
@Bean
public RealBean getRealBean() {
return new MockBean();
}
}
Can anyone explain why I need to change this method name and where I can find all these rules? It takes a very long time to troubleshoot these "why isn't my MockContext
working correctly?" issues.
FWIW, here's how I'm using this context in a test:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {RealContext.class, MockContext.class})
@WebAppConfiguration
public abstract class AbstractIntegrationTest {
And my integration test will extend this class. I am using Spring Boot 1.2.4.RELEASE