gpt4 book ai didi

java - Mockito - stub 抽象父类方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:07:12 29 4
gpt4 key购买 nike

我看到试图 stub 方法的行为很奇怪 myMethod(param)MyClass在抽象父类中定义的 MyAbstractBaseClass .

当我尝试 stub (使用 doReturn("...").when(MyClassMock).myMethod(...) 等)时,此方法失败,在不同情况下会抛出不同的异常。异常会在该行抛出。

当我使用 doReturn("...").when(MyClassMock).myMethod(CONCRETE PARAM CLASS OBJECT) 时,我得到以下异常:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
String cannot be returned by hasValidExpirationDate()
hasValidExpirationDate() should return boolean
at ...

hasValidExpirationDate()不是被 stub 的方法,而是被 MyMethod(param) 的实际实现调用在抽象基类中。

当我使用 doReturn("...").when(MyClassMock).myMethod(any(PARAMCLASS.class)) 时,我得到以下异常:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
0 matchers expected, 1 recorded.
This exception may occur if matchers are combined with raw values:

但是当我定义方法时 myMethod(param)在子类中 MyClass代码不再失败。我的具体实现在MyClass只需调用 super.myMethod(param)并返回它,所以它除了修复单元测试外没有任何作用。所以看起来 Mockito 只能 stub 定义在被模拟的类中的方法,而不是在父类(super class)中。

我正在阅读 Mockito 文档,但我没有看到它在哪里说不能 stub 继承的方法。

myMethod(param)既不是 static也不final .

代码:

BaseCard :

import java.io.Serializable;

public class BaseCard implements Serializable {

public boolean hasValidExpirationDate() {
return true;
}
}

Card :

abstract class Card  extends BaseCard {

public Card () { }

public String getUnexpiredStringForNetwork(){

//If the date is invalid return empty string, except for Discover.
if( ! hasValidExpirationDate()){
return "hi";
}

return "hello";
}
}

DecryptedCard :

public class DecryptedCard extends Card {

}

MyTest :

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import org.junit.Test;


public class MyTest {

@Test
public void test() {
DecryptedCard decryptedCardMock = mock(DecryptedCard.class);

doReturn("ABC").when(decryptedCardMock).getUnexpiredStringForNetwork();

}

}

失败:

org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
String cannot be returned by hasValidExpirationDate()
hasValidExpirationDate() should return boolean
***
If you're unsure why you're getting above error read on.
Due to the nature of the syntax above problem might occur because:
1. This exception *might* occur in wrongly written multi-threaded tests.
Please refer to Mockito FAQ on limitations of concurrency testing.
2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies -
- with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.

at Card.getUnexpiredStringForNetwork(Card.java:10)
at DecryptedCard.getUnexpiredStringForNetwork(DecryptedCard.java:1)
at MyTest.test(MyTest.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

最佳答案

根据 this SO answer ,当父类是非公共(public)的时,模拟行为无法保证,如 issue 212 中所述.

(感谢 Brice 在其他线程中的良好回答,并感谢 Vladimir、JB Nizet 和 acdcjunior 在评论线程中分享调试进度!)

关于java - Mockito - stub 抽象父类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19915270/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com