如何测试方法是否测试func和嘲笑函数有不同的参数
我通过Mockito有一个功能和单位测试。我在检查论点时很难。这是功能代码:
import API;
public class Controller {
private Service service;
public void testingFunction(String path, String name){
String user = API.get(); // the function gets the userName from a API
this.service.putInformation(path, name, user);
}
}
这是我的testingfunction
的测试仪:
@Mock
Service service;
@InjectMocks
Controller controller;
private static final String TEST_USER = "userName";
private static final String PATH = "test path";
private static final String NAME = "test name";
@Test
public void valid_test() {
controller.testingFunction(PATH, NAME);
Mockito.verify(service).putInformation(eq(PATH), eq(NAME), eq(TEST_USER));
}
但是,我发现参数不同的错误!实际调用有不同的参数
。不同的参数是用户,因为一个是test_user,一个是null。
它可以通过Anystring()传递。但是,我担心如果没有争论检查,该测试不够严格。 我不确定如何模拟API,因为它是外部的。
谢谢您的建议!
I have a function and a unit test of it via mockito. I have trouble in checking the arguments. Here is the function code:
import API;
public class Controller {
private Service service;
public void testingFunction(String path, String name){
String user = API.get(); // the function gets the userName from a API
this.service.putInformation(path, name, user);
}
}
And here is my tester for the testingFunction
:
@Mock
Service service;
@InjectMocks
Controller controller;
private static final String TEST_USER = "userName";
private static final String PATH = "test path";
private static final String NAME = "test name";
@Test
public void valid_test() {
controller.testingFunction(PATH, NAME);
Mockito.verify(service).putInformation(eq(PATH), eq(NAME), eq(TEST_USER));
}
However, I got the error that Argument(s) are different! Actual invocation has different arguments
. The different argument is USER because one is TEST_USER and one is null.
It can be passed by the anyString(). However, I'm worried that the test isn't strict enough without argument check.
I'm not sure how to mock the API because it's external.
Thank you for any suggestions!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论