英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论