how to handle exceptions in junit

前端 未结 4 1928
鱼传尺愫
鱼传尺愫 2020-12-09 08:14

I wrote some test cases to test some method. But some methods throw an exception. Am I doing it correctly?

private void testNumber(String word, int number) {         


        
4条回答
  •  醉话见心
    2020-12-09 08:57

    Remove the try-catch block and add throws Exception to your test method, like:

    @Test
    public final void testZero() throws Exception {
        assertEquals("zero",  service.convert(0));
    }
    

    JUnit expects failing tests will throw Exceptions, your catching them is just stopping JUnit from being able to report them properly. Also this way the expected property on the @Test annotation will work.

提交回复
热议问题