-1

How to verify that exception was thrown by mock method in code below?
It simply throw exception on checking method without ending verify.

// import mockito

...

@Test
public void someTest() throws Exception {
    // reset and setup mock
    reset(mock);
    when(mock.getObj(Matchers.anyLong()))
        .thenReturn(new Obj());
    when(mock.save(any(Obj.class)))
        .thenThrow(new RuntimeException("Error!"));
    // work where mock is used (it throws no exceptions)
    work();
    // verify that exception on mock.save() was thrown
    // ! PROBLEM HERE: exception throws right here and verify doesn't end  
    verify(mock, times(1)).save(any(Obj.class));
}

UPD
work() - only sends message to Kafka-consumer (which is being tested) which works on embedded Kafka-server.
mock - mocks some object in consumer logic.

In this case, checking out the exception is an ADDITIONAL check for checking a certain branch of the consumer algorithm (other asserts not important (deleted): they checks that message was worked).

slider
  • 421
  • 1
  • 4
  • 19
  • Possible duplicate of [How do you assert that a certain exception is thrown in JUnit 4 tests?](https://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests) – jonrsharpe May 29 '19 at 10:27
  • 1
    Or are you asking how to test that the mock does the thing you set it up to do, in which case: why do you think you need to test that? – jonrsharpe May 29 '19 at 10:28

1 Answers1

2

I assume that "work" is throwing the RuntimeException?

If so, you could surround your work() method with a try catch, for example...

try {
    work();
    Assert.fail("Did not catch expected exception!");
} catch(RuntimeException ex) {
    // Expected
}

verify(mock, times(1)).save(any(Obj.class));

If not, you may need to post the code under the test to let us see what is happening...


EDIT: Still not 100% sure what you mean, this test passes for me...

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class Stack {
    @Mock
    private Mocked mock;

    @Test
    public void someTest() throws Exception {
        reset(mock);
        when(mock.getObj(Matchers.anyLong()))
                .thenReturn(new Obj());
        when(mock.save(any(Obj.class)))
                .thenThrow(new RuntimeException("Error!"));
        work();
        verify(mock, times(1)).save(any(Obj.class));
    }

    private void work() {
        Obj obj = mock.getObj(1234L);

        try {
            mock.save(obj);
        } catch(Exception ex) {
            // Bad things happened
        }
    }

    private interface Mocked {
        Obj getObj(long l);

        Obj save(Obj obj);
    }

    public static class Obj {

    }
}
BretC
  • 4,141
  • 13
  • 22
  • Thanx for answer, but as written in the question, this method does not throw any exceptions. – slider May 29 '19 at 10:26
  • So not even the RuntimeException (which does not have to be declared in the method signature)? – BretC May 29 '19 at 10:26
  • Yes. All exceptions is catched in work(). I can detect it only by mock. – slider May 29 '19 at 10:31
  • 1
    @slider you don't test the mock, you test that *the thing you're actually testing* does the right thing in response. So... what *is* that right thing? That's your assertion. – jonrsharpe May 29 '19 at 10:37
  • Thanx a lot. You code is working and it's imitates my case, but my test still fails on verify(). Will look further... – slider May 29 '19 at 11:35
  • Error message is "Error!", as expected by when(). – slider May 29 '19 at 13:06
  • @slider So the exception is being thrown out of the "work" method which implies you are not catching it in the work method. It might be worth editing your question and including the actual code you are running because it's just guesswork otherwise – BretC May 29 '19 at 13:35