Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checked exception is invalid for this method [duplicate]

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.

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.

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.

like image 290
Sawyer Avatar asked Aug 24 '17 13:08

Sawyer


People also ask

Which of the following is are the checked exception 1 IOException 2 SQLException 3 ArithmeticException 4 NullPointerException?

3. Examples of checked exceptions are IOException, SQLException, ClassNotFoundException, etc whereas, examples of unchecked exceptions are ArithmeticException, ClassCastException, NullPointerException, IllegalArgumentException, etc. 4.

What is an example of a checked exception?

ClassNotFoundException, IOException, SQLException etc are the examples of the checked exceptions.

How do you handle checked exceptions?

A checked exception must be handled either by re-throwing or with a try catch block, whereas an unchecked isn't required to be handled. A runtime exception is a programming error and is fatal whereas a checked exception is an exception condition within your code's logic and can be recovered or re-tried from.

What is a checked exception in Java?

A checked exception in Java represents a predictable, erroneous situation that can occur even if a software library is used as intended. For example, if a developer tries to access a file, the Java IO library forces them to deal with the checked FileNotFoundException.


2 Answers

You are getting unit testing with mocking wrong. Here:

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

There is no point in mocking the class that is under test!

When you mock that class, you get a stub that 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 verify for example that the logger saw that expected logging call. In other words: you either use a mock to allow your code under test to do its job (with you being in control!) or to verify that 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!

like image 101
GhostCat Avatar answered Oct 07 '22 09:10

GhostCat


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.

like image 27
Heri Avatar answered Oct 07 '22 09:10

Heri