public class MockedLoader
extends org.springframework.test.context.support.DelegatingSmartContextLoader
MockedLoader can be used in unit tests to automatically create mocks for all autowired beans that are not yet defined in the spring context. To be used in combination with TestConfig classes. Example use:
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@ContextConfiguration(classes = MyTest.TestConfig.class, loader = MockedLoader.class)
public class MyTest {
@Autowired
private ClassBeingTested classBeingTested;
...
@Configuration
public static class TestConfig {
// Only the ClassBeingTested should be manually created with the actual implementation.
// All autowired beans that the ClassBeingTested depends on, will be replaced by a mock
// by the MockedLoader. Only beans for which a specific implementation should be used
// need to be specified in the TestConfig.
@Bean
public ClassBeingTested classBeingTested() {
return new ClassBeingTested();
}
}
}
Note: MockedLoader is based on Mockito mocking.
| Constructor and Description |
|---|
MockedLoader() |
| Modifier and Type | Method and Description |
|---|---|
org.springframework.context.ApplicationContext |
loadContext(org.springframework.test.context.MergedContextConfiguration mergedConfig) |
void |
processContextConfiguration(org.springframework.test.context.ContextConfigurationAttributes configAttributes) |
getAnnotationConfigLoader, getXmlLoaderpublic org.springframework.context.ApplicationContext loadContext(org.springframework.test.context.MergedContextConfiguration mergedConfig)
throws Exception
loadContext in interface org.springframework.test.context.SmartContextLoaderloadContext in class org.springframework.test.context.support.AbstractDelegatingSmartContextLoaderExceptionpublic void processContextConfiguration(org.springframework.test.context.ContextConfigurationAttributes configAttributes)
processContextConfiguration in interface org.springframework.test.context.SmartContextLoaderprocessContextConfiguration in class org.springframework.test.context.support.AbstractDelegatingSmartContextLoaderCopyright © 2017. All rights reserved.