- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
实现类中的逻辑如下所示,我尝试编写一个模拟并获取异常作为 WrongTypeOfReturnValue
/**
* update audit and courier log when Provider sign the document.
* @param cmsCourierInfo
* @param documentInfo
* @throws Exception
*/
public boolean doAuditProviderESignCompletion(CMSCourierInfo cmsCourierInfo, DocumentInfo documentInfo) throws Exception {
String signerTitle = "";
List<ParticipantInfo> participantInfos = documentInfo.getParticipants().getParticipantInfo();
//First participant is Provider
ParticipantInfo participantInfo = participantInfos.get(0);
if(StringUtils.isNotEmpty(participantInfo.getTitle())){
signerTitle = participantInfo.getTitle();
}
//update audit log with provider signed information
CMSCourierContractManager.signContract(cmsCourierInfo.getGuidString(), participantInfo.getName(), signerTitle, null);
//update audit log when document sent to Internal Signer
return doAuditSentForInternalSigner(cmsCourierInfo.getDocumentKey(), documentInfo);
}
测试
@Test
public void testUpdateContractStatus() throws Exception{
String documentKey = "TestKey";
cmsCourierInfo = mock(CMSCourierInfo.class);
when(cmsFactoryManager.findCourier(anyString())).thenReturn(cmsCourierInfo);
EchoSignDocumentServiceImpl echoSignDocumentService = spy(documentService);
when(echoSignDocumentService.updateContractStatus(anyString())).thenReturn(true);
doThrow(new RuntimeException()).when(echoSignDocumentService).updateContractStatus(anyString());
boolean status = echoSignDocumentService.updateContractStatus(documentKey);
Assert.assertEquals(true, status);
}
我得到的错误是
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Boolean cannot be returned by findCourier()
findCourier() should return CMSCourierInfo
***
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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
一些输入会有很大帮助
最佳答案
我无法解释您遇到的具体错误(我可能不得不查看更多代码)。但是在测试中肯定存在编码错误。在您调用 updateContractStatus()
之前,您告诉 mock 在使用任何字符串值调用此方法时抛出异常。因此,您永远不会得到分配的返回值,也永远不会到达 assert 语句。
此外,因为我无法看到您的所有代码,我只是在猜测,但根据发布的错误帮助消息,您可以尝试将 spy 类的 stub 更改为以下形式:
doReturn(true).when(echoSignDocumentService).updateContractStatus(anyString());
关于java - Mockito 抛出 WrongTypeOfReturnValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26409731/
我使用 JpaRepository 将集合保存到我的数据库中。这是方法的声明: List save(Iterable entities); 在我的测试中,我使用 returnsFirstArg 作为
我有两个时间: when(mock.returnList()).thenReturn(); when(injectedMock.returnFloat()).thenReturn(float); 该错
实现类中的逻辑如下所示,我尝试编写一个模拟并获取异常作为 WrongTypeOfReturnValue /** * update audit and courier log when P
我最近才开始使用 Mockito(或为此进行模拟/ stub )。我开始了解如何模拟对象,但我坚持使用以下代码: public class ConveyorBeltTest { private Lay
我的测试 List myList; @Test public void testIsValidPerson() { myList = new ArrayList(); myList.a
我正在尝试执行简单的单元测试,但它仍然抛出 null 或一些奇怪的错误,例如: org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Tick
错误详情: org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Boolean cannot be returned by updateIt
有这个简单的片段。 @Component public class SomeDependency { public Optional getSomeInt(String string) {
我尝试为我的演示者创建测试,但是当我运行它时,我收到了这种错误 org.mockito.exceptions.misusing.WrongTypeOfReturnValue: ScalarSynchr
我是一名优秀的程序员,十分优秀!