用不同的参数模拟相同的方法 [英] Mock same method with different parameters

查看:61
本文介绍了用不同的参数模拟相同的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mockito来测试我的业务服务,并且它使用了我想模拟的实用程序.每个服务方法中至少有2-3个针对具有不同参数的实用程序的调用.

I am using mockito to test my business service, and it uses a utility that i want to mock. there are at least 2-3 calls in each service method for utility with different arguments.

是否有建议的方法来将多个 when(...).thenReturn(...)用于同一方法但参数不同?

Is there any recommended way to use multiple when(...).thenReturn(...) for same method but different arguments?

我还想在内部使用 any() marcher.有可能吗?

I also want to use any() marcher as well inside. Is it possible?

更新:示例代码.

@Test
public void myTest() {
  when(service.foo(any(), new ARequest(1, "A"))).thenReturn(new AResponse(1, "passed"));
  when(service.foo(any(), new ARequest(2, "2A"))).thenReturn(new AResponse(2, "passed"));
  when(service.foo(any(), new BRequest(1, "B"))).thenReturn(new BResponse(112, "passed"));

  c.execute();
}

public class ClassUnderTest {
  Service service = new Service();
  public void execute() {
    AResponse ar = (AResponse) service.foo("A1", new ARequest(1, "A"));
    AResponse ar2 = (AResponse) service.foo("A2", new ARequest(2, "2A"));
    BResponse br = (BResponse) service.foo("B1", new BRequest(1, "B"));
  }
}

public class Service {
  public Object foo(String firstArgument, Object obj) {
    return null; //return something
  }
}

推荐答案

一种方法是避免对参数进行过于严格的限制,以便仅通过一个 thenReturn 调用即可提供所有预期的结果.

One way could be to avoid being too restrictive on your arguments in order to provide all the expected results with only one thenReturn call.

例如,假设我要模拟此方法:

For example let's say that I want to mock this method:

public String foo(String firstArgument, Object obj) {
    return "Something";
}

然后可以通过提供所需的结果来模拟它,如下所示:

You could then mock it by providing as many results as you want like below:

// Mock the call of foo of any String to provide 3 results
when(mock.foo(anyString(), anyObject())).thenReturn("val1", "val2", "val3");

使用任何参数调用 foo 都会分别提供" val1 "," val2 ",然后所有后续调用都将提供" val3 ".

Calls to foo with any parameters will provide respectively "val1", "val2", then any subsequent calls will provide "val3".

如果您确实关心传递的值但不想依赖调用顺序,则可以使用 thenAnswer 来提供与第二个参数匹配的答案,就像您当前所做的那样,但可以使用3个不同的参数 thenReturn .

In case you do care about passed values but don't want to depend on call sequence you can use thenAnswer to provide an answer that matches with the second argument like you currently do but with 3 different thenReturn.

when(mock.foo(anyString(), anyObject())).thenAnswer(
    invocation -> {
        Object argument = invocation.getArguments()[1];
        if (argument.equals(new ARequest(1, "A"))) {
            return new AResponse(1, "passed");
        } else if (argument.equals(new ARequest(2, "2A"))) {
            return new AResponse(2, "passed");
        } else if (argument.equals(new BRequest(1, "B"))) {
            return new BResponse(112, "passed");
        }
        throw new InvalidUseOfMatchersException(
            String.format("Argument %s does not match", argument)
        );
    }
);

这篇关于用不同的参数模拟相同的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆