user3257736 user3257736 - 3 months ago 15x
Java Question

ReflectionTestUtils.setField (Mockito), not recognizing field.

I am trying to test a Spring Boot Controller by using Mockito. I am following this tutorial:

The method I am testing is:

public class DigipostSpringConnector {

private String statusQueryToken;

public String whenSigningComplete(@RequestParam("status_query_token") String token){
this.statusQueryToken = token;

So far, I have written this in my test-class:

public class DigipostSpringConnectorTest {

public void whenSigningCompleteSetsToken() throws Exception{
DigipostSpringConnector instance = new DigipostSpringConnector();
ReflectionTestUtils.setField(instance, "statusQueryToken", statusQueryToken);


However, I get the error "Cannot resolve symbol statusQueryToken", It seems like the test does not know that I am referring to the private field statusQueryToken, which is in another class.

Any ideas on how to solve this?

Thank you!


It is because value variable statusQueryToken in whenSigningCompleteSetsToken() method is not defined. Try this:

String statusQueryToken = "statusQueryToken"; 
ReflectionTestUtils.setField(instance, "statusQueryToken", statusQueryToken);