問題描述
通常在使用 mockito 時,我會做類似的事情
Normally when using mockito I will do something like
Mockito.when(myObject.myFunction(myParameter)).thenReturn(myResult);
有沒有可能做一些類似的事情
Is it possible to do something along the lines of
myParameter.setProperty("value");
Mockito.when(myObject.myFunction(myParameter)).thenReturn("myResult");
myParameter.setProperty("otherValue");
Mockito.when(myObject.myFunction(myParameter)).thenReturn("otherResult");
所以而不是僅僅使用參數來確定結果.它使用參數內的屬性值來確定結果.
So rather than when just using the parameter to determine the result. It is using a value of a property inside the parameter to determine the result.
所以當代碼執行時,它的行為是這樣的
so when the code is executed it behaves like so
public void myTestMethod(MyParameter myParameter,MyObject myObject){
myParameter.setProperty("value");
System.out.println(myObject.myFunction(myParameter));// outputs myResult
myParameter.setProperty("otherValue");
System.out.println(myObject.myFunction(myParameter));// outputs otherResult
}
<小時>
目前的解決方案,希望可以提出更好的建議.
current solution, hopefully something better can be suggested.
private class MyObjectMatcher extends ArgumentMatcher<MyObject> {
private final String compareValue;
public ApplicationContextMatcher(String compareValue) {
this.compareValue= compareValue;
}
@Override
public boolean matches(Object argument) {
MyObject item= (MyObject) argument;
if(compareValue!= null){
if (item != null) {
return compareValue.equals(item.getMyParameter());
}
}else {
return item == null || item.getMyParameter() == null;
}
return false;
}
}
public void initMock(MyObject myObject){
MyObjectMatcher valueMatcher = new MyObjectMatcher("value");
MyObjectMatcher otherValueMatcher = new MyObjectMatcher("otherValue");
Mockito.when(myObject.myFunction(Matchers.argThat(valueMatcher))).thenReturn("myResult");
Mockito.when(myObject.myFunction(Matchers.argThat(otherValueMatcher))).thenReturn("otherResult");
}
推薦答案
這是一種方法.這使用 Answer
對象來檢查屬性的值.
Here's one way of doing it. This uses an Answer
object to check the value of the property.
@RunWith(MockitoJUnitRunner.class)
public class MyTestClass {
private String theProperty;
@Mock private MyClass mockObject;
@Before
public void setUp() {
when(mockObject.myMethod(anyString())).thenAnswer(
new Answer<String>(){
@Override
public String answer(InvocationOnMock invocation){
if ("value".equals(theProperty)){
return "result";
}
else if("otherValue".equals(theProperty)) {
return "otherResult";
}
return theProperty;
}});
}
}
還有一種我更喜歡的替代語法,它可以實現完全相同的效果.由你選擇其中的哪一個.這只是 setUp
方法 - 測試類的其余部分應該與上面相同.
There's an alternative syntax, which I actually prefer, which will achieve exactly the same thing. Over to you which one of these you choose. This is just the setUp
method - the rest of the test class should be the same as above.
@Before
public void setUp() {
doAnswer(new Answer<String>(){
@Override
public String answer(InvocationOnMock invocation){
if ("value".equals(theProperty)){
return "result";
}
else if("otherValue".equals(theProperty)) {
return "otherResult";
}
return theProperty;
}}).when(mockObject).myMethod(anyString());
}
這篇關于基于參數屬性的模擬返回值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!