Search code examples
javaspringunit-testingjunitspring-java-config

JUnit Test Cases for Config


I have a javaconfig file that looks like this:

@Configuration
public class ServiceConfig {
    @Autowired
    FooBean someBean;

    @Bean
    @Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public FooService fooService() {
        return new FooServiceImpl(someBean, fooB());
    }

    private Foo fooB() {
        return new FooB();
    }
}

And I have created a junit test file like so based on this stack answer :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ServiceConfig.class)
public class ServiceConfigTest {

}

But I have a couple questions:

  1. Should I test all my config files with this one junit test file? I have 4 config files in total including the ServiceConfig file, so in the ContextConfiguration should I just list them all out or have a junit test for each one indivually?

  2. What am I supposed to test here? I tried reading this spring guide but I'm not really understand what behavior I should test here....just if something gets autowired successfully?


Solution

  • Should I test all my config files with this one junit test file? I have 4 config files in total including the ServiceConfig file, so in the ContextConfiguration should I just list them all out or have a junit test for each one indivually?

    In your test class, @ContextConfiguration must be positioned on the root of the class. So for testing each configuration, you have to create a test class by Configuration.

    What am I supposed to test here? I tried reading this spring guide but I'm not really understand what behavior I should test here....just if something gets autowired successfully?

    Testing if autowired is successful seems not very useful. It would be as unit test that Spring feature works. If you want to unit test these classes, you should test your own processing. For the moment, you have not really. So, I am not sure that testing them has great value.