如何解决不必要的存根异常

How to resolve Unneccessary Stubbing exception(如何解决不必要的存根异常)
本文介绍了如何解决不必要的存根异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我的代码如下,

@RunWith(MockitoJUnitRunner.class)公共类 MyClass {私有静态最终字符串代码=测试";@嘲笑私人 MyClassDAO 道;@InjectMocks私有 MyClassService 服务 = new MyClassServiceImpl();@测试公共无效 testDoSearch() 抛出异常 {最终字符串 METHOD_NAME = logger.getName().concat(".testDoSearchEcRcfInspections()");CriteriaDTO dto = 新 CriteriaDTO();dto.setCode(代码);检查服务.searchEcRcfInspections(dto);列出<SearchCriteriaDTO>summaryList = new ArrayList();检查SummaryList.add(dto);when(dao.doSearch(dto)).thenReturn(inspectionsSummaryList);//在这一行出错验证(dao).doSearchInspections(dto);}}

我遇到了异常

<块引用>

org.mockito.exceptions.misusing.UnnecessaryStubbingException:在测试类中检测到不必要的存根:测试清洁&可维护的测试代码需要零个不必要的代码.以下存根是不必要的(单击以导航到相关代码行):1.->在 service.Test.testDoSearch(Test.java:72)请删除不必要的存根或使用静默"选项.更多信息: UnnecessaryStubbingException 类的 javadoc.在 org.mockito.internal.exceptions.Reporter.formatUnncessaryStubbingException(Reporter.java:838)在 org.mockito.internal.junit.UnnecessaryStubbingsReporter.validateUnusedStubs(UnnecessaryStubbingsReporter.java:34)在 org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:49)在 org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:103)在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

请帮我解决一下

解决方案

首先你应该检查你的测试逻辑.通常有3种情况.首先,您在模拟错误的方法(您打错字或有人更改了测试代码,因此不再使用模拟的方法).其次,在调用此方法之前您的测试失败了.第三,你的逻辑在代码中的某个地方出现了错误的 if/switch 分支,因此不会调用模拟方法.

如果这是第一种情况,您总是希望将模拟方法更改为代码中使用的方法.对于第二个和第三个,这取决于.如果它没有用,通常你应该删除这个模拟.但有时在参数化测试中存在某些情况,应该采取不同的路径或提前失败.然后您可以将此测试拆分为两个或多个单独的测试,但这并不总是很好看.可能带有 3 个参数提供程序的 3 种测试方法会使您的测试看起来不可读.在这种情况下,对于 JUnit 4,您可以使用以下任一方式来消除此异常

@RunWith(MockitoJUnitRunner.Silent.class)

注释或者如果您使用的是规则方法

@Rule公共 MockitoRule 规则 = MockitoJUnit.rule().strictness(Strictness.LENIENT);

或(相同的行为)

@Rule公共 MockitoRule 规则 = MockitoJUnit.rule().silent();

对于 JUnit 5 测试,您可以使用 mockito-junit-jupiter 包中提供的此注释来消除此异常:

@ExtendWith(MockitoExtension.class)@MockitoSettings(严格 = Strictness.LENIENT)类 JUnit5MockitoTest {}

My Code is as below,

@RunWith(MockitoJUnitRunner.class)
public class MyClass {

    private static final String code ="Test";

    @Mock
     private MyClassDAO dao;

    @InjectMocks
     private MyClassService Service = new MyClassServiceImpl();

    @Test
     public void testDoSearch() throws Exception {
         final String METHOD_NAME = logger.getName().concat(".testDoSearchEcRcfInspections()");
         CriteriaDTO dto = new CriteriaDTO();
         dto.setCode(code);
         inspectionService.searchEcRcfInspections(dto);
         List<SearchCriteriaDTO> summaryList = new ArrayList<SearchCriteriaDTO>();
         inspectionsSummaryList.add(dto);
         when(dao.doSearch(dto)).thenReturn(inspectionsSummaryList);//got error in this line
         verify(dao).doSearchInspections(dto);

      }
}

I am getting below exception

org.mockito.exceptions.misusing.UnnecessaryStubbingException: 
Unnecessary stubbings detected in test class: Test
Clean & maintainable test code requires zero unnecessary code.
Following stubbings are unnecessary (click to navigate to relevant line of code):
  1. -> at service.Test.testDoSearch(Test.java:72)
Please remove unnecessary stubbings or use 'silent' option. More info: javadoc for UnnecessaryStubbingException class.
  at org.mockito.internal.exceptions.Reporter.formatUnncessaryStubbingException(Reporter.java:838)
  at org.mockito.internal.junit.UnnecessaryStubbingsReporter.validateUnusedStubs(UnnecessaryStubbingsReporter.java:34)
  at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:49)
  at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:103)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Please help me how to resolve

解决方案

At first you should check your test logic. Usually there are 3 cases. First, you are mocking the wrong method (you made a typo or someone changed tested code so that mocked method is no longer used). Second, your test is failing before this method is called. Third, your logic falls in wrong if/switch branch somewhere in the code so that mocked method is not called.

If this is the first case you always want to change the mocked method for the one used in the code. With the second and the third it depends. Usually you should just delete this mock if it has no use. But sometimes there are certain cases in parametrized tests, which should take this different path or fail earlier. Then you can split this test into two or more separate ones but that's not always good looking. 3 test methods with possibly 3 arguments providers can make your test look unreadable. In that case for JUnit 4 you silent this exception with either

@RunWith(MockitoJUnitRunner.Silent.class) 

annotation or if you are using rule approach

@Rule
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.LENIENT);

or (the same behaviour)

@Rule
public MockitoRule rule = MockitoJUnit.rule().silent();

For JUnit 5 tests you can silence this exception using this annotation provided in mockito-junit-jupiter package:

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class JUnit5MockitoTest {
}

这篇关于如何解决不必要的存根异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Sending a keyboard event from java to any application (on-screen-keyboard)(将键盘事件从 java 发送到任何应用程序(屏幕键盘))
How to make JComboBox selected item not changed when scrolling through its popuplist using keyboard(使用键盘滚动其弹出列表时如何使 JComboBox 所选项目不更改)
Capturing keystrokes without focus(在没有焦点的情况下捕获击键)
How can I position a layout right above the android on-screen keyboard?(如何将布局放置在 android 屏幕键盘的正上方?)
How to check for key being held down on startup in Java(如何检查在Java中启动时按住的键)
Android - Get keyboard key press(Android - 获取键盘按键)