How make JUnit print assertion results

前端 未结 4 854
说谎
说谎 2021-02-01 03:48

How can I get the results of my JUnit assertions to be printed [to standard output]?

I have some tests like this:

@Test
public void test01()
{
    Position         


        
4条回答
  •  孤独总比滥情好
    2021-02-01 03:53

    All the assertXXX methods have a form that allows for displaying a String on error:

    assertNotNull("exists a2", p); // prints "exists a2" if p is null

    There is no particular value in printing a message on success.

    EDIT

    Junit typically provides 2 forms of an assert. To follow the example above, you can test for a null value in 1 of 2 ways:

    assertNotNull(p)

    or

    assertNotNull("my message on failure", p)

    The framework will print the error messages with no other effort required by you (it's provided by the framework).

    To test for exceptions you would use the following pattern:

    try{
        someCall();
    catch(Exception e){
        fail(): // exception shouldn't happen, use assertTrue(true) if it should
    }
    

    Again, there are versions of these methods for adding a message

    Check the API

提交回复
热议问题