gpt4 book ai didi

java - Mockito 异常 : Missing method invocation while trying to test an if conditon

转载 作者:行者123 更新时间:2023-11-30 06:49:43 25 4
gpt4 key购买 nike

我正在尝试使用 when(),thenReturn() 来测试 if 条件,但是当我运行我的测试用例时,即使我已经模拟了实现该方法的类,它也会让我缺少方法调用。

这是我要模拟的 if 条件

     if(request.getProcessType() == IPRequest.IPREQUEST_TYPE_TOMO_RECON)//IPREQUEST_TYPE_TOMO_RECON=9, this is the condition I am trying to test    
{
params.setTubeAngle(accessor); //I am verifying if these methods are invoked
params.setTomoFocalSpot(accessor);
}

这就是我检查 if 条件的方式

when(request.getProcessType()).thenReturn(IPRequest.IPREQUEST_TYPE_TOMO_RECON);
Mockito.verify(ipImgParam,Mockito.times(3)).setTubeAngle(Mockito.any(AttributeExtractor.class));

我已经使用@Mock 批注模拟了“请求”,但仍然出现以下异常。

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
2. inside when() you don't call method on mock but on some other object.
3. the parent of the mocked class is not public.
It is a limitation of the mock engine.

at common.systemreprocessingservice.test.ImageParamsBuilderTest.testbuildProcessingInfoIf(ImageParamsBuilderTest.java:134)

我不确定我做错了什么。谁能帮忙?

最佳答案

我不能写评论,这就是为什么要在这里问。什么样的对象是“请求”?如果它是最终类的对象,那么您将需要 PowerMockito 来模拟它。

另外,使用@Mock注解后,是否在运行测试用例前调用了MockitoAnnotations.initMocks(testClass.class)方法?

以下链接介绍了模拟对象的不同方式。 https://blog.frankel.ch/initializing-your-mockito-mocks/#gsc.tab=0

关于java - Mockito 异常 : Missing method invocation while trying to test an if conditon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42383414/

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