Friday, 4 March 2016

JUNIT provides static methods in the Assert class to test for the certain conditions.These assert statements start with keyword "assert" and allow you to specify the error message,expected result and actual result.
An assertion method compares the actual value returned by the test to the expected value and throws an AssertionException if the comparison fails.
Below are few methods in which parameters in [] bracket are optional and of type string.

1. Fail(message)

This method says - let the method fail.
This method is used to check that a certain part of the code is not reachable or to have a failing test before the test code was implemented.
Let us try to understand it more with the help of an example.
Untitled drawing (1)


2. assertTrue([message],boolean condition)

This method checks that the boolean condition is true.
It would be more clear in the following example.
Untitled drawing (23)

3. assertFalse([message],boolean condition)

This method checks that the boolean condition is false.
Lets take the following example. Below method asserts that a condition is false, if it is not false than it throws an AssertionError with the given message.
Untitled drawing (26).jpg

4. assertequals([message],expected,actual)

This method tests two values are same or not.
For arrays, reference is checked and not the content of the arrays.
Lets take the following example. Here, the expected value(ONETWO) is same as actual value(result == ONETWO). Hence this test case would pass.

Code snippet -

public class Test{
@Test
public void testConcatenate(){
MyUnit obj = new MyUnit();
String result = myUnit.concatenate("ONE", "TWO");

assertEquals("ONETWO", result);
}
}
Untitled drawing (2)

5. assertEquals([message],expected,actual,tolerence)

This method tests that float or double value matches or not. The tolerence is the number of decimals which must be same.
Tolerence -  Take the following example:
abc = 1/3;
xyz = 1/3 + 1 - 1;
Simple math says these should be the same value, but if you use
assertEquals(abc,xyz), the assertion will fail. Thus use assertEquals function that accepts a tolerance like so: assertEquals("",abc,xyz,0.01). This would pass the
assertion if the two values were within 1 one-hundredth of each other
Code snippet for the assertEquals with tolerance is as follows -
Untitled drawing (4)

6. assertNull([message],object)

This method checks that the object is null .
Untitled drawing (5)
Here we checked whether the value of "obj" was null or not . If yes it is null(which is the case)our method would pass.

7. assertNotNull([message],object)

This method checks that the object is not null.
Untitled drawing (6).jpg

8. assertSame([message],expected,actual)

This method checks that both variables refer to the same object.
Untitled drawing (7).jpg

9. assertNotSame([message],expected,actual)

This method checks that both variables refer to the different objects.
Untitled drawing (8)

10. assertThat()

This method compares an object to org.hamcrest.Matcher to see if the give object matches whatever the Matcher requires it to match.
MATCHER - Matchers is an external addition to the JUnit framework. Matchers were added by the framework called Hamcrest. JUnit 4.8.2 ships with Hamcrest internally, so you don't have to download it.
Untitled drawing (9)
It is the Matcher that determines if the test passes or fails.

11. assertArrayEquals(expectedArray,resultArray)

This method will test whether two arrays are equal to each other.
Untitled drawing (10)