Mock an Autowired Value Field in Spring with Junit 5

Mock @Value

In this example I am going to show you how to mock an autowired field in Spring boot framework with Junit 5. When you use @Value annotation in Spring beans to read the value from properties file and later when you want to perform Junit test then you also need to pass value for this autowired field value otherwise you won’t be able to cover your testing. Read if you need Junit 4 version of testing for Spring’s autowired field value.

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.

Actually you need to pass three parameters as values into setField() method. The first parameter’s value indicates the class instance for which you want to set value to the autowired 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.


Java 8/12/19, Maven 3.6.3/3.8.5, Spring Boot 2.4.0 – 2.4.5/3.1.3

Project Setup

You can create a maven based project in your favorite IDE or tool. The name of the project is spring-mock-autowired-field-value-junit-5.

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

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

<project xmlns=""










Spring Service

I am creating a simple Spring service class that has an autowired @Value annotated field that is set to a value from properties file (src/main/resources/ This @Value annotated field will be mocked in the Junit class later in this example.

public class SpringServiceAutowiredField {

    private String securityKey;

    public void getValue() {
        System.out.println("${security.key}: " + securityKey);


Properties File

I am creating a properties file src/main/resources/ file with the following key/value pair.


As I have created Spring boot application so first I will verify this autowired @Value field using main class.

Spring Boot Main Class

In Spring Boot application a class with main method and @SpringBootApplication annotation is enough to run the application. I am using CLI version of Spring boot application.

public class SpringMockAutowiredValueApp implements CommandLineRunner {

    private SpringServiceAutowiredField service;

    public static void main(String[] args) {, args);

    public void run(String... args) throws Exception {


Testing the @Value Field

Just executing the above main class will give you the following output in the concole.

${security.key}: SecurityKey

Next let’s move on to creating Junit test class that will test the @Value annotated field.

Junit Class

The following class in created under src/test/java folder.

public class SpringAutowiredValueTest {

    private SpringServiceAutowiredField service;

    public void setUp() {
        ReflectionTestUtils.setField(service, "securityKey", "It's a security key");

    public void testAutowiredValueField() {
        Mockito.verify(service, Mockito.times(1)).getValue();


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 @BeforeAll annotation and if you need to execute before each test method then you need to put it using @BeforeEach annotation.

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

Here you run the class with @ExtendWith(MockitoExtension.class) because you do not want to run integration test but mock test.

Notice how I do initialize the autowired @Value annotated 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.

Testing the Junit Test Class

Now run the Junit class, your Junit test will pass.

mock an autowired value field in spring with junit 5

You will get the following output in the console.

Setting field 'securityKey' of type [null] on target object [service] or target class [class com.roytuts.spring.mock.autowired.field.value.junit.SpringServiceAutowiredField$MockitoMock$1287953113] to value [It's a security key]

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

Source Code


Leave a Reply

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