How to get an output from JUnit assertEquals() method and put it into a variable?

前端 未结 2 1841
南旧
南旧 2021-01-22 08:11

I have a JUnit test case here to test a simple class. Basically the class contains just one method called \'sum\' that returns the sum of two numbers. To test if this is right I

2条回答
  •  一整个雨季
    2021-01-22 08:40

    assertEquals is a void method. You cannot put the result to variable. But you can get the exception with try/catch block.

    try{
      assertEquals("foo", "foo1"); //will fail
    } catch(AssertionError e){
      String message = e.getMessage();
      //do whatever you want with e
    }
    

提交回复
热议问题