How make JUnit print assertion results

前端 未结 4 851
说谎
说谎 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

    0 讨论(0)
  • 2021-02-01 04:05

    First, you have two issues not one. When an assertion fails, an AssertionError exception is thrown. This prevents any assertion past this point from being checked. To address this you need to use an ErrorCollector.

    Second, I do not believe there is any way built in to JUnit to do this. However, you could implement your own methods that wrap the assertions:

    public static void assertNotNull(String description, Object object){
         try{
              Assert.assertNotNull(description, object);
              System.out.println(description + " - passed");
         }catch(AssertionError e){
              System.out.println(description + " - failed");
    
            throw e;
         }
    }
    
    0 讨论(0)
  • 2021-02-01 04:08

    Existing Answers/Comments here contain enough info to understand how to print something based on JUnit assertions - but they also explain how doing so is probably not what you actually want to do, and is probably missing the point of running unit tests in the first place.

    You should be viewing the results of the tests themselves, instead of trying to print something while you don't understand how/where to view test results themselves.

    Now then how/where to view results themselves depends on how you are running your tests - you need to understand how you are running your tests, and then research how to view test results according to how you are running them. Here are a few (but not limited to) examples:

    • Running tests in IntelliJ
    • Running tests in Eclipse
    • Running tests on command line
    • Running tests in Jenkins
    0 讨论(0)
  • 2021-02-01 04:14

    One last resort option is to pair each assert with a corresponding System.out.println, though obviously that is less than ideal. Still, it will solve the problem if all else fails.

    0 讨论(0)
提交回复
热议问题