Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Unit Test @Value from .properties File gives NullPointerException

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.

like image 840
shinvu Avatar asked Oct 21 '25 01:10

shinvu


1 Answers

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());
    }
}

like image 97
shazin Avatar answered Oct 25 '25 08:10

shazin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!