D
D
DeNissss44442021-08-27 15:07:02
Java
DeNissss4444, 2021-08-27 15:07:02

Which method in JUnit checks if a RuntimeException() is being thrown?

I am learning to write simple tests in jUnit5 and the question arose how can I test a method if it should return throw new RuntimeException() ? I mean what should the test look like which should check whether the program (which I am testing) will cause a RuntimeException() or not? I know that there is an assertNull which checks that an object is empty, but is there something similar for other cases of RuntimeException() ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DeNissss4444, 2021-08-27
@DeNissss4444

Maybe someone in the future will come in handy with the documentation for JUnit5, the solution is something like this

@Test
    void checkingFor() {
        String input = "text";
        Exception exception = assertThrows(RuntimeException.class, () ->
                balanceVerificator.checkBalanceBrackets(input) );
        assertEquals("тут пишете точный текст ошибки который должен появиться", exception.getMessage());
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question