The below example will show you how to mock an Autowired @Value field in Spring with Junit Mockito. Generally we read some configuration values from properties file into Spring bean or component class using @Value annotated attributes but when we 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 mechanism to set values to aitowired@Value fields using RefectionTestUtils’s setField() method. Basically you need to pass three parameters as values into this method. The first 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 third 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 autowired@Value field in Spring with Junit mockito framework.

Prerequisites

Knowledge of Java, Junit, Spring

Softwares

JDK 1.8, Spring, Gradle

Eclipse, Junit, Mockito

Generally when we mock such field that is being used for entire class, we 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.

In the below implementation we assume that you have a basic spring project setup and you have following dependencies in your gradle script or maven pom file:

Junit and spring test framework

Now we will move on to the example…

Here we create one Spring service class that has an autowired @Value field and we will mock this field in our Junit class. We have also a simple method in this service class to verify the value of the autowired field we initialize into junit class.

Spring Service class

@Service
public class SpringValueServiceImpl {

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

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

Junit test case

We create the below junit class for the above service class.

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

Notice how we do initialize our autowired field using ReflectionTestUtils.

We 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 SpringValueServiceImplTest {

    @Spy
    private final SpringValueServiceImpl springValueServiceImpl = new SpringValueServiceImpl();

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

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

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

That’s all. Hope you got an idea how to mock an autowired @Value field in Spring with Junit mockito.

Thanks for reading.

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Roy Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

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