Java 已检查异常对此方法无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45862953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:12:36  来源:igfitidea点击:

checked exception is invalid for this method

javajunitmockitopowermockito

提问by Sawyer

I have the below class There is an answer to this in StackOverflow but it deals with List throw checked Exceptions from mocks with Mockito. I like to look into this condition. Not getting where I am missing.

我有下面的类 StackOverflow 中有一个答案,但它处理 List throw checked Exceptions from mocks with Mockito。我喜欢研究这种情况。没有到达我失踪的地方。

    public SimpleClass{
       private SimpleClass(){}
       public void runMethod(request,String,Map,Object,Object){
             try{
                  doesSomething()....
                }
         }
        catch(Exception e){
             String message= ""+request.getAttribute(X)+"Some message"
              Logger.Log(param1+param2+message);
           }

      }

My Test method looks like below. I trying to run the coverage with the JUnit but the Catch Block is not covered, so Wrote the below test method. It throws the below exception. Not able to get where I am missing.

我的测试方法如下所示。我试图用 JUnit 运行覆盖但没有覆盖 Catch 块,所以写了下面的测试方法。它抛出以下异常。无法到达我失踪的地方。

  public class SimpleClassTest{
      @Test
       public void testCatchBlock(){
           SimpleClass instanceObj =PowerMockito.mock(SimpleClass.class);
           Mockito.doThrow(new Exception()).when(instanceObj).runMethod(request, anyString(), anyMap(), anyObject(), anyObject());
      }
   }

Exception Thrown

抛出异常

   org.mockito.exceptions.base.MockitoException: 
   Checked exception is invalid for this method!
   Invalid: java.lang.Exception

Edit

编辑

I am able to run the method by giving NullPointerException. When I try for code coverage with Junit, the catch block is completely shown as red, and the catch phrase is shown yellow. How do I achieve 100% coverage and how to test the String message in the catch block.

我可以通过给出 NullPointerException 来运行该方法。当我尝试使用 Junit 进行代码覆盖时,catch 块完全显示为红色,而标语则显示为黄色。如何实现 100% 覆盖以及如何测试 catch 块中的 String 消息。

采纳答案by GhostCat

You are getting unit testing with mocking wrong. Here:

您正在通过模拟错误进行单元测试。这里:

SimpleClass instanceObj =PowerMockito.mock(SimpleClass.class);

There is nopoint in mocking the class that is under test!

没有在嘲笑的类,它是点测试

When you mockthat class, you get a stubthat has "nothing to do" with your real implementation. A "working setup" would look more like:

当您模拟该类时,您会得到一个与您的实际实现“无关”的存根。“工作设置”看起来更像是:

public void methodUnderTest(X x, ...) {
   try { 
     x.foo(); 
   } catch (Exception e) {
    ...
}

and

 X mockedX = mock(X.class);
 when(x.foo()).thenThrow(new WhateverException());

 underTest.methodUnderTest(mockedX); ...

and then you could try to verifyfor example that the logger saw that expected logging call. In other words: you either use a mock to allowyour code under test to do its job (with youbeing in control!) or to verifythat some expected call took place on a mock object.

然后您可以尝试验证例如记录器是否看到了预期的日志记录调用。换句话说:你要么使用模拟来允许你的被测代码完成它的工作(由控制!)要么验证一些预期的调用发生在模拟对象上。

But as said: it doesn't make any sense to mock that class that you want to test. Because a mocked object doesn't know anything about the "real" implementation!

但正如所说:模拟您要测试的类没有任何意义。因为模拟对象对“真实”实现一无所知!

回答by Heri

Manipulate the environment so that the "doesSomething()" throws the Exception you want. Since we do not know what "doesSomething()" realy does one cannot say more.

操纵环境,以便“doesSomething()”抛出您想要的异常。由于我们不知道“doesSomething()”到底做了什么,所以不能多说。