如何在不运行方法的情况下模拟方法调用和返回值?

时间:2022-04-27 22:43:33

Consider the following method:

考虑以下方法:

public boolean isACertainValue() {
        if(context.getValueA() != null && context.getValueA().toBoolean() == true) {
        if(context.getType() != null && context.getType() == ContextType.certainType) {
            return true;
        }
    }
    return false;
}

I did not write this code, it is ugly as hell, it is totally overcomplicated but I have to work with it.

我没有写这段代码,它太丑了,太复杂了,但我必须要处理它。

Now I want to test a method that relies on a call to this method.

现在我想测试一个依赖于对这个方法的调用的方法。

I thought I could deal with this by:

我想我可以这样处理:

Mockito.when(spy.isACertainValue()).thenReturn(true); because that's the case I want to test.

Mockito.when(spy.isACertainValue()).thenReturn(真正的);因为这是我想测试的。

But it doesn't work as it is still calling the method-body :/

但它并不起作用,因为它仍在调用方法体:/

I get nullpointers or rather I get something along the lines of

我得到了零指针,或者更确切地说,我得到了沿着

misusing.WrongTypeOfReturnValue; Boolean cannot be returned by getValueA(). getValueA() should return ValueA

misusing.WrongTypeOfReturnValue;不能通过getValueA()返回布尔值。应该返回ValueA getValueA()

So I tried (as a workaround) to do:

所以我试着(作为一个变通方法)去做:

Mockito.when(contextMock.getValueA()).thenReturn(new ValueA()); and Mockito.when(contextMock.getType()).thenReturn(ContextType.certainType);

Mockito.when(contextMock.getValueA())。thenReturn(新ValueA());和Mockito.when(contextMock.getType()).thenReturn(ContextType.certainType);

but then I get a nullpointer that I cant seem to be able to debug.

但是我得到了一个nullpointer,我似乎无法调试它。

So, how is it done right in this case?

那么,在这种情况下,怎么做才是正确的呢?

2 个解决方案

#1


15  

When you are invoking

当你调用

Mockito.when(spy.isCertainValue()).thenReturn(true);

the method isCertainValue() is getting called here. This is how Java works: to evaluate the argument of Mockito.when, the result of spy.isCertainValue() must be evaluated so the method must be called.

这里调用的方法isCertainValue()。这就是Java的工作原理:评估Mockito的论点。当,必须计算sp . iscertain value()的结果,因此必须调用该方法。

If you don't want that to happen, you can use the following construct:

如果您不希望这种情况发生,您可以使用以下结构:

Mockito.doReturn(true).when(spy).isCertainValue();

This will have the same mocking effect but the method won't be called with this.

这将具有相同的mock效果,但不会使用此方法调用该方法。

#2


0  

This code is correct:

这段代码是正确的:

Mockito.when(contextMock.getType()).thenReturn(ContextType.certainType);

But you are getting NullPointerException because you didn't define the Mocking value that should be defines, well I'm using Spring, in my context file when I define @Autowired bean I define it this way:

但是你会得到NullPointerException因为你没有定义应该定义的mock值,我在我的上下文文件中使用Spring,当我定义@Autowired bean时,我是这样定义的:

<bean id="contextMock" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="com.example.myspringproject.bean.ContextMock" />
</bean>

#1


15  

When you are invoking

当你调用

Mockito.when(spy.isCertainValue()).thenReturn(true);

the method isCertainValue() is getting called here. This is how Java works: to evaluate the argument of Mockito.when, the result of spy.isCertainValue() must be evaluated so the method must be called.

这里调用的方法isCertainValue()。这就是Java的工作原理:评估Mockito的论点。当,必须计算sp . iscertain value()的结果,因此必须调用该方法。

If you don't want that to happen, you can use the following construct:

如果您不希望这种情况发生,您可以使用以下结构:

Mockito.doReturn(true).when(spy).isCertainValue();

This will have the same mocking effect but the method won't be called with this.

这将具有相同的mock效果,但不会使用此方法调用该方法。

#2


0  

This code is correct:

这段代码是正确的:

Mockito.when(contextMock.getType()).thenReturn(ContextType.certainType);

But you are getting NullPointerException because you didn't define the Mocking value that should be defines, well I'm using Spring, in my context file when I define @Autowired bean I define it this way:

但是你会得到NullPointerException因为你没有定义应该定义的mock值,我在我的上下文文件中使用Spring,当我定义@Autowired bean时,我是这样定义的:

<bean id="contextMock" class="org.mockito.Mockito" factory-method="mock">
    <constructor-arg value="com.example.myspringproject.bean.ContextMock" />
</bean>