Monday, 12 September 2016

Junit Assert Statements

As we all know that test case is incomplete without assertions. Assertions means checking expected result matches actual result that is always final step of any test case. So JUnit testing framework provides us way to automate our assertions in simple and effective way.

Junit Provides Assert Class with some assert methods which we can use write our own assert statements.It allows us to specify error message with expected and actual result so that we can understand the why the test case failed.

The Assert Statement compare actual result with Expected result and if the comparison fails then it throws AssertionException.

Let us see some commonly used assertions example  -

1. assertTrue([message,] boolean condition) - Checks that the boolean condition is true.
2. assertFalse([message,] boolean condition) - Checks that the boolean condition is false.
3. assertEquals([message,] expected, actual) - Tests that two values are the same. Note: for arrays the reference is checked not the content of the arrays. 
4. assertEquals([message,] expected, actual, tolerance) - Test that float or double values match. The tolerance is the number of decimals which must be the same.
4. assertNull([message,] object) - Checks that the object is null. 
Whenever you start writing test cases you can explore more more just by Typeing Assert.(cltr+space) the you will see the lis of methods provided by Assert class and you can use them to make your test cases more and more effective.
 If you have any questions then you can put it in comments.


Post a Comment