如何测试方法是否测试func和嘲笑函数有不同的参数

发布于 2025-02-13 19:11:27 字数 1000 浏览 0 评论 0原文

我通过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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文