4

I am trying to read a value from a properties file for a unit test case in Spring Boot. I have two config.properties files, one in src/main/resources:

prop = some-value

and one in src/test/resources:

prop = some-test-value

Main Application class:

package company.division.project;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication(scanBasePackages = "company.division.project")
@PropertySource(value = "classpath:config.properties")
public class Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        System.setProperty("DUMMY_PROPERTY", "dummy-value");

        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        // Do nothing with main
    }
}

Service class to be tested:

package company.division.project.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class Service {
    @Autowired
    Environment environment;

    public String getProperty() {
        return environment.getProperty("prop");
    }

}

ServiceTest class. I have tried two approaches to retrieving the value in the src/test/resources/config.properties file; one with an @Autowired Environment, and one with an @Value annotation...neither worked:

package company.division.project.service;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.test.context.TestPropertySource;

@RunWith(MockitoJUnitRunner.class)
@TestPropertySource("classpath:config.properties")
public class ServiceTest {
    @InjectMocks
    Service service;

    @Autowired
    Environment environment;

    @Value("${prop}")
    private String expectedProperty;

    @Test
    public void testGetPropertyWithValueAnnotation() {
        assertEquals(expectedProperty, service.getProperty());
    }

    @Test
    public void testGetPropertyWithEnvironment() {
        assertEquals(environment.getProperty("prop"), service.getProperty());
    }
}

I read somewhere on StackOverflow, that in order to auto-wire components in a Spring test class, I'll need to create an entire context for the test, so I tried this (change the annotations and test runner):

package company.division.project.service;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.env.Environment;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {
    @InjectMocks
    Service service;

    @Autowired
    Environment environment;

    @Value("${prop}")
    private String expectedProperty;

    @Test
    public void testGetPropertyWithValueAnnotation() {
        assertEquals(expectedProperty, service.getProperty());
    }

    @Test
    public void testGetPropertyWithEnvironment() {
        assertEquals(environment.getProperty("prop"), service.getProperty());
    }
}

The context was created, but both approaches ended in NullPointerExceptions once again.

shinvu
  • 601
  • 2
  • 7
  • 23
  • `@RunWith(MockitoJUnitRunner.class)` won't work. What is the exact exception message? – Alan Hay Sep 23 '19 at 12:45
  • This is the error message (in every case): `java.lang.NullPointerException at company.division.project.service.ServiceTest.testGetPropertyWithValueAnnotation(ServiceTest.java:34)` – shinvu Sep 23 '19 at 12:52
  • Why won't `@RunWith(MockitoJUnitRunner.class)` work? – shinvu Sep 23 '19 at 12:54

2 Answers2

3

The problem with your test is that you are trying to use to MockitoJUnitRunner.class in a wrong way.

If you are mocking a Service using @InjectMocks you need to make sure you need to return the value Service.getProperty() by mocking the service call. If you are using SpringRunner.class then you shouldn't have @InjectMocks but should have @Autowired for the service. Following test works.

@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {
    @Autowired
    Service service;

    @Autowired
    Environment environment;

    @Value("${prop}")
    private String expectedProperty;

    @Test
    public void testGetPropertyWithValueAnnotation() {
        assertEquals(expectedProperty, service.getProperty());
    }

    @Test
    public void testGetPropertyWithEnvironment() {
        assertEquals(environment.getProperty("prop"), service.getProperty());
    }
}

shinvu
  • 601
  • 2
  • 7
  • 23
shazin
  • 21,379
  • 3
  • 54
  • 71
  • This worked perfectly on the minimal example I posted here, but when I try it on my original code, it gives me an `IllegalStateException: Unable to find a @SpringBootConfiguration`. Any ideas? – shinvu Sep 25 '19 at 06:27
  • @shinvu `@SpringBootTest` searches for the class annotated by `@SpringBootApplication.`. You can either give your main class explicitly by `@SpringBootTest(classes=YourMainClass.class)`, or putting your test in the same or child pacakge of your SpringBootApplication annotated class. – helospark Sep 25 '19 at 07:52
  • @helospak Perfect! Adding the main class explicitly worked. Thanks. – shinvu Sep 26 '19 at 05:48
1

Thanks to @shazin's answer and some of my own research I've been able to solve the problem.

Basically, there needs to be compatibility between the test runner class specified in @RunWith and the annotations for the Mockito mocks. We want to test the Service class:

Service Class:

@Component
public class Service {
    @Autowired
    Environment environment;

    public String getProperty() {
        return environment.getProperty("prop");
    }
}

If you're using @RunWith(MockitoJUnitRunner.class), you can use the @InjectMocks and @Mock annotations like below. Whatever is @Autowired in Service will be auto-wired with the mocks:

Test Class with MockitoJUnitRunner:

@RunWith(MockitoJUnitRunner.class)
public class ServiceTest {
    @InjectMocks
    Service service;
        @Mock
        Environment mockEnvironment;

    @Before
    public void before() {
        Mockito.when(mockEnvironment.getProperty("prop")).thenReturn("some-test-value")
    }
}

But you can't auto-wire anything in the test class itself. That requires a Spring Context (a Spring Context is needed to manage the beans which get auto-wired into objects). That's where @RunWith(SpringRunner.class) comes into the picture. You can use it to run a test case with a dedicated Spring context (you'll notice the test case logs showing a new Spring application being booted up for every test class with the @RunWith(SpringRunner.class) annotation). You'll also need to provide the Configuration details with the @SpringBootTest annotation.

The caveat is that a test class with @RunWith(SpringRunner.class) won't understand the @InjectMocks and @Mock annotations; you'll have to use the @MockBean annotation. This will effectively modify the Spring context by replacing beans with their mocks; anything with the @Autowired annotation will get auto-wired with the mock beans automatically:

Test Class with SpringRunner:

@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class ServiceTest {
    @Autowired
    Service service;

    @MockBean
    Environment mockEnvironment;

    @Before
    public void before() {
        Mockito.when(mockEnvironment.getProperty("prop")).thenReturn("some-test-value")
    }
}

So...using the @RunWith(SpringRunner.class) didn't achieve anything except change the names of the annotations (@InjectMocks -> @Autowired, and @Mock -> @MockBean), right? Wrong. Using SpringRunner gives you the power of auto-wiring components within your test case. So if you want to use an actual Environment (not a mock one), you can do that as well; just auto-wire it in from the dedicated Spring context:

Test Class with SpringRunner and @Autowired Environment:

@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class ServiceTest {
    @Autowired
    Service service;

    @Autowired
    Environment environment;

    @Test
    public void testServiceGetProperty() {
        assertEquals(environment.getProperty("prop"), service.getProperty("prop");
    }

}

And that solves the problem.

shinvu
  • 601
  • 2
  • 7
  • 23