What's the difference between failure and error in JUnit?

后端 未结 8 2494
粉色の甜心
粉色の甜心 2020-11-30 06:27

I\'m running JUnit tests on a large code base, and I\'ve been realizing that sometimes I get \"Errors\" while other times I get \"Failures\". What\'s the difference?

8条回答
  •  醉话见心
    2020-11-30 06:54

    Below test explains the difference between Test Error vs Test failure.

    I have commented the line which throws test error and test failure.

        @Test
        public void testErrorVsTestFailure() {
    
            final String sampleString = null;
    
            assertEquals('j', sampleString.charAt(0) );
            //above line throws test error as you are trying to access charAt() method on null reference
    
            assertEquals(sampleString, "jacob");
            //above line throws Test failure as the actual value-a null , is not equal to expected value-string "jacob"
            }
    

    So Junit shows test error whenever you get an exception , and test failure when your expected result value doesn't match your actual value

提交回复
热议问题