Search code examples
javaspringunit-testingspring-bootjunit

Spring Test cannot get Spring Configuration Beans


@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
@ActiveProfiles(profiles = "test")
@RunWith(MockitoJUnitRunner.class)
public class ConfigurationTest {
    @Autowired
    private Environment environment;

    @Test
    public void test1() throws Exception {
        environment.getProperty("bhavya",Boolean.class);
    }

    @Configuration
    @Profile("test")
    @ComponentScan(basePackages={"com.bhavya.test"})
    public static class EnvironmentServiceTestConfiguration{

        @Bean
        @Primary
        public Environment environment(){
           return Mockito.mock(Environment.class);
        }
    }
}

I also tried putting EnvironmentServiceTestConfiguration as a non-inner non-static class, but didn't help.

Here is what I tried in a separate class:

@RunWith(MockitoJUnitRunner.class)
@Profile("test")
@Configuration
class EnvironmentServiceTestConfiguration{

    @Bean
    @Primary
    public Environment environment(){
        return Mockito.mock(Environment.class)
    }
}

didn't work either

The test class is located in test/java/com.bhavya.test package. I am trying to run this particular test test1

This is my first test of such kind. I have never before used AnnotationConfigContextLoader.class, enlighten me.

Stacktrace :

java.lang.NullPointerException

at Line number where I have statement :

environment.getProperty("bhavya",Boolean.class);

Solution

  • Try using @RunWith(SpringJUnit4ClassRunner.class)

    Here is an example explaining how can be done https://www.mkyong.com/unittest/junit-spring-integration-example/

    Here is a code sample that works for the sample in the question:

    package com.example.demo.test;

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Mockito;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.core.env.Environment;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    import org.springframework.test.context.support.AnnotationConfigContextLoader;
    
    import static org.mockito.Matchers.eq;
    import static org.mockito.Mockito.when;
    
    @ContextConfiguration(loader = AnnotationConfigContextLoader.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    public class ConfigurationTest {
        @Autowired
        private Environment environment;
    
        @Test
        public void test1() throws Exception {
            System.out.println(environment.getProperty("bhavya",Boolean.class));
        }
    
        @Configuration
        @ComponentScan(basePackages={"com.example.demo.test"})
        public static class EnvironmentServiceTestConfiguration{
    
            @Bean
            @Primary
            public Environment environment(){
                Environment env =  Mockito.mock(Environment.class);
                when(env.getProperty(eq("bhavya"), eq(Boolean.class))).thenReturn(true);
    
                return env;
            }
        }
    }