dimanche 24 février 2019

Testing misuse exceptions in mockito

I wanted to test a misuse exception in mockito just for educational purposes to demonstrate the error:

    @Test
    public void testNotAMockException() {
        try {
            List<String> list = new ArrayList<String>();
            Mockito.doReturn(100).when(list).size();

            fail("Should have thrown a NotAMockException because list is not a mock!");
        } catch (NotAMockException e) {
            assertThat(e.getMessage(), containsString("Argument passed to when() is not a mock!"));
        }

    }

This behaves correctly and the assertion passes, but then the test fails with a org.mockito.exceptions.misusing.UnfinishedStubbingException

I understand why but is there a way I can suppress the TestFailue being fired by Mockito at the end?

Thanks

Aucun commentaire:

Enregistrer un commentaire