0

I have to write Junit test for the catch block. But I am not able to identify what should I assert here. Since the func() is only catching the exception and not throwing anything I cannot assert using Assertions.assertThatExceptionOfType(). I am new to Junit testing so cannot think of anything else. Any possible way to test the type of exception received by catch block.

Method

public void func() {
    try {
        int x = solve();
    } catch(Exception1 e) {
        log.warn("error", e);
    } catch(Exception2 e) {
        log.warn("error", e);
    }
}

private int solve() throws ExceptionName {
    //do something...
    throws new Exception("error occured");
    ...
}
Shaggy31
  • 35
  • 1
  • 5
  • Here, you are not calling func method. Are you using Mockito with JUnit? – Ismail Oct 06 '20 at 10:30
  • As I can see in the current example, the tested module uses a logger to log some msg. You have at least two options: 1) Configure the logger is such manner so you are able to capture and validate the logged message. 2) Use mock/stub of the logger itself and validate the passed (message, exception) tuple. – dbl Oct 06 '20 at 10:32
  • @dbl log will be passing event ID so 1st option is not possible. If some how I can mock the logger then how will I get the exception type coz it is catching more than one exceptions. – Shaggy31 Oct 06 '20 at 10:57
  • I can not give you further details as I will have to dig a bit more into it, but here is at least a starting point - https://stackoverflow.com/questions/8948916/mocking-logger-and-loggerfactory-with-powermock-and-mockito – dbl Oct 06 '20 at 11:47

1 Answers1

0

You can change the visibility of solve() method and test it with all exception cases. For example change it to default

int solve() throws ExceptionName {

Put tests in the same package as class with this method so that it could be access from test.

UPDATE

The best way would be to change the code to be more testable as it was shown above. In order to not change the code you can use the way from this answer. It can be tricky. With Mockito and PowerMockito you can control when Exception1 or Exception2 are creating. Based on this you will know which catch statement was executed.

In test code it could be like this:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Exception1.class, Exception2.class, MyClass.class })
public class TestClass {

    @Before
    public void setup() {
        Exception1 cutMock = Mockito.mock(Exception1.class);
        PowerMockito.whenNew(Exception1.class)
                .withArguments(Matchers.anyString())
                .thenReturn(cutMock);
    }

    @Test
    public void testMethod() {
        // prepare
        MyClasss myClass = new MyClass();

        // execute
        myClass.func();

        // checks if the constructor has been called once and with the expected argument values:
        String value = "abc";
        PowerMockito.verifyNew(Exception1.class).withArguments(value);
    }
}
dbl
  • 1,109
  • 9
  • 17
lczapski
  • 4,026
  • 3
  • 16
  • 32