[英]ReflectionTestUtils.setField (Mockito), not recognizing field.
I am trying to test a Spring Boot Controller by using Mockito. 我试图通过使用Mockito测试Spring Boot Controller。 I am following this tutorial: https://www.javacodegeeks.com/2013/07/getting-started-with-springs-mvc-test-framework-part-1.html
我正在关注本教程: https : //www.javacodegeeks.com/2013/07/getting-started-with-springs-mvc-test-framework-part-1.html
The method I am testing is: 我正在测试的方法是:
public class DigipostSpringConnector {
@Autowired
private String statusQueryToken;
@RequestMapping("/onCompletion")
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 {
@Before
public void whenSigningCompleteSetsToken() throws Exception{
MockitoAnnotations.initMocks(this);
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. 但是,我收到错误“无法解析符号statusQueryToken”,似乎测试不知道我指的是私有字段statusQueryToken,它在另一个类中。
Any ideas on how to solve this? 关于如何解决这个问题的任何想法?
Thank you! 谢谢!
It is because value variable statusQueryToken in whenSigningCompleteSetsToken() method is not defined. 这是因为未定义whenSigningCompleteSetsToken()方法中的值变量statusQueryToken。 Try this:
尝试这个:
String statusQueryToken = "statusQueryToken";
ReflectionTestUtils.setField(instance, "statusQueryToken", statusQueryToken);
statusQueryToken
is undefined, simply because you haven't defined it. statusQueryToken
未定义,只是因为您尚未定义它。 The third parameter to setField()
defines which value you want to assign to the field. setField()
的第三个参数定义了要分配给字段的值。 So, you should do something like: 所以,你应该做的事情如下:
ReflectionTestUtils.setField(instance, "statusQueryToken", "the string value to set");
Replace "the string value to set"
with whatever you want to assign to the field. 将
"the string value to set"
替换"the string value to set"
要分配给字段的任何内容。
ReflectionTestUtils
will then, with the help of reflection , search in instance
for a field called statusQueryToken
, and assign the value "the string value to set"
to it. 然后,
ReflectionTestUtils
将在反射的帮助下,在instance
搜索名为statusQueryToken
的字段,并为其指定值"the string value to set"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.