首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >基于参数属性的mockito返回值

基于参数属性的mockito返回值
EN

Stack Overflow用户
提问于 2014-03-12 07:03:06
回答 5查看 81.5K关注 0票数 77

通常,在使用mockito时,我会这样做

代码语言:javascript
复制
Mockito.when(myObject.myFunction(myParameter)).thenReturn(myResult);

有没有可能做一些类似的事情

代码语言:javascript
复制
myParameter.setProperty("value");
Mockito.when(myObject.myFunction(myParameter)).thenReturn("myResult");

myParameter.setProperty("otherValue");
Mockito.when(myObject.myFunction(myParameter)).thenReturn("otherResult");

因此,而不是仅仅使用参数来确定结果。它使用参数内的属性值来确定结果。

因此,当执行代码时,它的行为如下所示

代码语言:javascript
复制
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
}

目前的解决方案,希望能有更好的建议。

代码语言:javascript
复制
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");
}
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-03-12 16:36:26

这里有一种方法。它使用Answer对象来检查属性的值。

代码语言:javascript
复制
@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方法-测试类的其余部分应该与上面的相同。

代码语言:javascript
复制
@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());
}
票数 61
EN

Stack Overflow用户

发布于 2016-12-26 16:07:49

在Java 8中,它甚至比上面的所有操作都更简单:

代码语言:javascript
复制
when(mockObject.myMethod(anyString()))
    .thenAnswer(invocation -> 
        invocation.getArgumentAt(0, String.class));
票数 95
EN

Stack Overflow用户

发布于 2014-03-12 07:12:34

是的,您可以使用自定义参数匹配器。

有关更多详细信息,请参阅the javadoc of Matchers,更具体地说,请参阅ArgumentMatcher

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22338536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档