Unit Test a configuration class not working in Multi Maven Spring Boot project

v_anon :

I am working on multi module maven project with SpringBoot framework. The project is divided in 4 modules: rest module,service module,repository module, domain module. I am trying to write a unit test for a configuration class in java which is located in the service module. I have simplified the case to get rid of business logic complexity. The configuration class is like below:

@Configuration
@ConfigurationProperties(prefix = "x.y.feature", ignoreInvalidFields = false)
public class FeatureConfig {

        private String featureUrl;

        public String getFeatureUrl() {
            return featureUrl;
        }

        public void setFeatureUrl(String FeatureUrl) {
            this.featureUrl = featureUrl;
        }

}

The properties file is application.properties.

x.y.feature.featureUrl=featureUrl

And below is the unit test that is not working.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { FeatureConfigTest.class })
public class FeatureConfigTest {
    @Autowired
    private FeatureConfig featureConfig;

    @Test
    public void testgetFeatureUrl() {
        String expected ="featureUrl";
        assertEquals(expected,featureConfig.getFeatureUrl());
    }
}

When i run the unit test , it throws the exception below:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
Rando Shtishi :

The problem is that you are trying to use object of FeatureConfig from the application context. You haven't initialized the application context. When you use the @Autowire annotation, you are mapping field 'featureConfig' with instance of type FeatureConfig class located in the application context. To bypass this error you need initialize the application context.

First thing , create static class inside the test class that will help us not to load the whole application context. Because for this test case you don't need to load the whole application. First thing , create static class inside the test class that will help us not to load the whole application context. Because for this test case you don't need to load the whole application.

    @EnableConfigurationProperties(FeatureConfig.class)
    public static class TestConfiguration {
    }

After that you start the application context , but with passing as configuration the static class you created. This is done not to load the whole application context.

@SpringBootTest(classes = { FeatureConfigTest.TestConfiguration class })

Copy and paste the changes below in your test class and everything should work ok.

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { FeatureConfigTest.TestConfiguration class })
public class FeatureConfigTest {
    @Autowired
    private FeatureConfig featureConfig;

    @Test
    public void testgetFeatureUrl() {
        String expected ="featureUrl";
        assertEquals(expected,featureConfig.getFeatureUrl());
    }

    @EnableConfigurationProperties(FeatureConfig.class)
    public static class TestConfiguration {
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=416187&siteId=1