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

2023-05-02Java开发问题
1966

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

问题描述

vpn下载 vpn下载 限时送ChatGPT账号.. free vpn vpn下载 免费vpn

我的代码如下,

@RunWith(MockitoJUnitRunner.class)公共类 MyClass 免费vpn {私有静态最终字符串代码=测试";@嘲笑私人 MyClassDAO 免费vpn vpn free 道;@InjectMocks私有 MyClassService 服务 = vpn下载 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)在 vpn下载 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)

请帮我解决一下

free vpn 解决方案

首先你应该检查你的测试逻辑.通常有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 测试,您可以使用 vpn free mockito-junit-jupiter 包中提供的此注释来消除此异常:

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

My Code is as below,

@RunWith(MockitoJUnitRunner.class)
public class MyClass {

  vpn下载   private static final String code ="Test";

    @Mock
   免费vpn   private MyClassDAO dao;

    @InjectMocks
   free vpn   免费vpn下载 private MyClassService Service = new MyClassServiceImpl();

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

      }
}

I am getting below exception

org.mockito.exceptions.misusing.UnnecessaryStubbingException: 
Unnecessary 免费vpn stubbings detected in test class: Test
Clean free vpn & maintainable test code requires zero 免费vpn下载 unnecessary code.
Following stubbings are vpn free unnecessary (click to navigate to relevant line of code):
  1. -> at service.Test.testDoSearch(Test.java:72)
Please remove unnecessary stubbings 免费vpn or use 'silent' option. vpn下载 免费vpn 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)
  免费vpn下载 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 vpn free 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 vpn下载 code so that mocked method is not called.

If this is the first case you always want to change vpn free the free vpn mocked method for the one used in the code. With the second and the third it depends. vpn free Usually you should just delete this mock if it has no use. But sometimes there are certain cases in parametrized vpn free 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 免费vpn not always good looking. 3 免费vpn下载 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 免费vpn approach

@Rule
public MockitoRule 免费vpn 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 {
}

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

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send 免费vpn下载 data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to free vpn make a report page direction to change to vpn下载 quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
vpn free 2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言? 免费vpn
Is there any way vpn free to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 免费vpn下载 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings vpn下载 in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13