Use a different Spring context configuration for different testing methods.

We have a Spring-based JUnit test class that uses the internal test context configuration class

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = ServiceTest.Config.class) public class ServiceTest { @Test public void someTest() { ... @Configuration @PropertySource(value = { "classpath:application.properties" }) @ComponentScan({ "..." }) public static class Config { ... 

Recently, new functions of the Service class have been added, for which the corresponding tests should be added to ServiceTest. However, for this, you will also need to create another testing context configuration class (the interiors of the existing configuration class are quite complex and changing it to serve both old and new tests will seem to be extremely complex, if possible at all)

Is there a way to achieve that some test methods in one test class will use one configuration class and other methods will use another? @ContextConfiguration seems to be applicable only at the class level, so the solution may be to create another test class for new tests that will use their own context configuration class; but that would mean that the same class of service extends through two different test classes

+5
source share
2 answers

I use these approaches when I need to solve this:

  • Manually create the context in the installation method instead of using annotations.
  • Move the general test code to the base class and extend it. This allows me to run tests with different spring contexts.
  • A mixture of the two above. The base class then contains methods for constructing spring contexts from fragments (which extensions can override). It also allows me to redefine test cases that make no sense or do extra pre / post work in some tests.

Keep in mind that annotations solve only general cases. You will have to reproduce some or all of their work when you leave the common language.

+1
source

With Aaron’s suggestion of manually creating a context, I couldn’t find any good examples, so after spending some time working on it, I thought I would send a simple version of the code that I used if it helps anyone else:

 class MyTest { @Autowired private SomeService service; @Autowired private ConfigurableApplicationContext applicationContext; public void init(Class<?> testClass) throws Exception { TestContextManager testContextManager = new TestContextManager(testClass); testContextManager.prepareTestInstance(this); } @After public void tearDown() throws Exception { applicationContext.close(); } @Test public void test1() throws Exception { init(ConfigATest.class); service.doSomething(); // assert something } @Test public void test2() throws Exception { init(ConfigBTest.class); service.doSomething(); // assert something } @ContextConfiguration(classes = { ConfigATest.ConfigA.class }) static class ConfigATest { static class ConfigA { @Bean public SomeService someService() { return new SomeService(new A()); } } } @ContextConfiguration(classes = { ConfigBTest.ConfigB.class }) static class ConfigBTest { static class ConfigB { @Bean public SomeService someService() { return new SomeService(new B()); } } } } 
+4
source

Source: https://habr.com/ru/post/1241817/


All Articles