基于参数字段的方法行为模拟 Java 8

huangapple go评论61阅读模式
英文:

Mocking method behaviour based on the field of the parameter Java 8

问题

以下是翻译好的部分:

我有一个类定义如下

public class MyClass {

    public performExecution(ParamsClass paramsClassObject);
}

public ParamsClass {
    private String field1;
    private String field2;
}

现在我想基于field1来模拟方法`performExecution`,但即使使用ArgumentMatchers也没有直接的方法来实现

假设我模拟了这个类

MyClass myClass = mock(MyClass.class)

现在我想执行类似以下的操作
`myClass.performExecution()` 中传递的`Params`对象具有`field1`的值为"1"提供特定的响应否则提供其他响应

有办法可以实现吗
英文:

I have a class definition as follows:

public class MyClass {

	public performExecution(ParamsClass paramsClassObject);
}

public ParamsClass {
	private String field1;
	private String field2;
}

Now, I want to mock the method performExecution based on the field1, but there is no direct way to do it even with the ArgumentMatchers.

Suppose I mocked the class:

MyClass myClass = mock(MyClass.class)

Now, I want to perform something like this:
when the Params object passed in myClass.performExecution() has the field1 value as "1", then give a certain response, otherwise some other response.

Is that a way to do that ?

答案1

得分: 0

有几个选项:

选项 1:自定义参数匹配器

查看例如 Custom Argument Matcher 由 baeldung 提供

选项 2:when / thenAnswer

@Test
void testThenAnswer() {
    MyClass myCollaborator = Mockito.mock(MyClass.class);
    when(myCollaborator.performExecution(any(ParamsClass.class)))
            .thenAnswer((invocationOnMock -> {
        ParamsClass params = invocationOnMock.getArgument(0);
        return "f1".equals(params.getField1())
                ? "V1"
                : "V2"
    }));

    var result = myCollaborator.performExecution(new ParamsClass("f1", "f2"));
    Assertions.assertThat(result).isEqualTo("V1");
}

选项 3:argThat 匹配器

@Test
void testArgThat() {
    MyClass myCollaborator = Mockito.mock(MyClass.class);
    when(myCollaborator.performExecution(Mockito.argThat(p -> p != null && "f1".equals(p.getField1())))).thenReturn("V1");
    when(myCollaborator.performExecution(Mockito.argThat(p -> p != null && !"f1".equals(p.getField1())))).thenReturn("V2");

    var result = myCollaborator.performExecution(new ParamsClass("f1", "f2"));
    Assertions.assertThat(result).isEqualTo("V1");
}

注意 argThat 匹配器中的空值检查

英文:

There are a couple of options:

Option 1: Custom argument matcher

See for example Custom Argument Matcher by baeldung

Option 2: when / thenAnswer

@Test
void testThenAnswer() {
    MyClass myCollaborator = Mockito.mock(MyClass.class);
    when(myCollaborator.performExecution(any(ParamsClass.class)))
            .thenAnswer((invocationOnMock -> {
        ParamsClass params = invocationOnMock.getArgument(0);
        return "f1".equals(params.getField1())
                ? "V1"
                : "V2";
    }));

    var result = myCollaborator.performExecution(new ParamsClass("f1", "f2"));
    Assertions.assertThat(result).isEqualTo("V1");
}

Option 3: argThat Matcher

@Test
void testArgThat() {
    MyClass myCollaborator = Mockito.mock(MyClass.class);
    when(myCollaborator.performExecution(Mockito.argThat(p -> p != null && "f1".equals(p.getField1())))).thenReturn("V1");
    when(myCollaborator.performExecution(Mockito.argThat(p -> p != null && !"f1".equals(p.getField1())))).thenReturn("V2");

    var result = myCollaborator.performExecution(new ParamsClass("f1", "f2"));
    Assertions.assertThat(result).isEqualTo("V1");
}

Note null checks in argThat matchers

huangapple
  • 本文由 发表于 2023年2月6日 18:52:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定