私はビジネスサービスをテストするためにmockitoを使用しています。私はモックしたいユーティリティを使用しています。異なる引数を持つユーティリティのために各サービスメソッドに少なくとも2〜3の呼び出しがあります。異なるパラメータを使用して同じメソッドをモックします
when(...).thenReturn(...)
を同じ方法でも引数を変えて複数使用する推奨方法はありますか?
また、any()
marcherも使用したいと思います。出来ますか?
更新:サンプルコード。
@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
}
}
これは矛盾します。 'any()'を指定すると、何かを意味しますが、同時に、あなたは?適切なマッチャーを使用してください。 – SpaceTrucker
どうか私にお勧めします。私はパラメータの1つのためのオブジェクトを作成することはできません、それは私がany()を使用する理由ですが、挑戦的に私は動作しません。私は同じことをしたいと言っていました。 –