Issue
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 NullPointerException
s once again.
Solution
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.
Answered By - shinvu