Mock an Autowired @Value field in Spring with Junit Mockito

Mock @Value Field

The below example will show you how to mock an Autowired @Value field in Spring with Junit Mockito. Generally you read some configuration values from properties file into Spring bean or component class using @Value annotated attributes but when you want to test such service or component class using Junit test class then it is required to pass values for those autowired fields.

Now it is really cumbersome to place a properties file and read configuration values into those fields. Therefore Spring provides an easy way to set values to aitowired @Value fields using RefectionTestUtils‘s setField() method.

Related Posts:

Basically you need to pass three parameters as values into this method. The first parameter’s value indicates the class instance for which you want to set value to the auto-wired field. The second parameter’s value indicates the class attribute name for which you want to set the value. The final or third parameter’s value indicates the actual value that will be set to the class attribute.

Therefore you are basically mocking the @Value attribute using Spring’s ReflectionTestUtils API.

The below example shows how to mock an auto-wired @Value field in Spring with Junit’s Mockito framework.

Prerequisites

Java 19, Junit 4.13.2, Spring Boot 3.1.3, Mockito 5.3.1, Hamcrest 2.2

Project Setup

Create a maven based project in your favorite IDE or tool. The name of the project is spring-mock-autowired-field-value-junit-4.

For the maven project you can use the following pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.roytuts</groupId>
	<artifactId>spring-mock-autowired-field-value-junit-4</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>19</maven.compiler.source>
		<maven.compiler.target>19</maven.compiler.target>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>3.1.3</version>
	</parent>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>junit</groupId>
					<artifactId>junit</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.junit.jupiter</groupId>
					<artifactId>junit-jupiter</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.junit.jupiter</groupId>
					<artifactId>junit-jupiter-api</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.mockito</groupId>
					<artifactId>mockito-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.hamcrest</groupId>
					<artifactId>hamcrest-core</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.hamcrest</groupId>
			<artifactId>hamcrest</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-core</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Service Class

Here I will create one Spring service class that has an autowired @Value field and I will mock this field in the Junit class. I have also defined a simple method in this service class to verify the value of the auto-wired field which I initialize into Junit class.

@Service
public class SpringService {

	@Value("${security.key}")
	private String securityKey;

	public void updateValue() {
		System.out.println(securityKey);
	}

}

Properties File

Create an application.properties file under classpath directory src/main/resources and put the below key/value pair into it.

security.key=SecurityKey

Junit Class

I will create the below Junit class for the above service class.

Generally when you mock such field that is being used for entire class. Therefore, you need to initialize such field only once or before each test case executed. So if you need to initialize only once then you can put it using @BeforeClass annotation and if you need to execute before each class then you need to put it using @Before annotation.

Here in this example I have initialized the field using @Before annotation because I have only one test case.

Here I run the class with MockitoJunitRunner because I do not want to run integration test but mock test.

Notice how I do initialize the autowired field using ReflectionTestUtils.

I finally test the service class’s method and verify whether the method executed at least once using Junit’s verify() method.

@RunWith(MockitoJUnitRunner.class)
public class SpringServiceTest {

	@Spy
	private final SpringService springJunitService = new SpringService();

	@Before
	public void setUp() {
		ReflectionTestUtils.setField(springJunitService, "securityKey", "it's a security key");
	}

	@Test
	public void testUpdateUser() throws Exception {
		springJunitService.updateValue();
		Mockito.verify(springJunitService, Mockito.times(1)).updateValue();
	}

}

You may read why I have used @Spy and Mockito.verify() in this example.

Testing the @Value Field

Now run the Junit class, your Junit test will pass and you will get the following output in the console.

I have set the new value to the security key and displayed using updateValue() method.

it's a security key

Source Code

Download

4 thoughts on “Mock an Autowired @Value field in Spring with Junit Mockito

  1. Hello Soumitra,
    I am using the above solution but didn’t work out for me. for me property value is still null when i run Junit test cases.

Leave a Reply

Your email address will not be published. Required fields are marked *