JUnit Annotations Example
JUnit Assert Class
JUnit Test Cases Class
JUnit TestResult Class
JUnit Test Suite Class

JUnit Annotations Example

Let’s create a class covering important JUnit annotations with simple print statements and execute it with a test runner class: Step 1) Consider below java class having various methods which are attached to above-listed annotations:

JunitAnnotationsExample.java

Step 2) let’s create a test runner class to execute above test:

TestRunner.java

Expected Result

All the test cases will be executed one by one, and all print statement can be seen on a console. As discussed in above table @Before annotation in JUnit, @BeforeClass [ method m1() and m2() ] will be executed before each and before all test cases respectively. In the same way @After in JUnit, @afterClass (method m3() and m4()) will be executed after each and after all test cases respectively. @ignore (method m6())will be treated as ignoring the test.

Let’s analyse test cases used in above java class in detail:

Consider method m5() as given below :

In above method as you are adding a string in the variable “list” so

list.isEmpty() will return false. assertFalse(list.isEmpty()) must return true. As a result, the test case will pass.

As you have added only one string in the list, so the size is one.

list.size() must return int value as “1” . So assertEquals(1, list.size()) must return true. As a result, the test case will pass.

Consider method m7() as given below :

As discussed above @Test(timeout = 10)annotation is used to enforce timeout in the test case.

Consider method m8() as given below :

As discussed above @Test(expected) will check for specified exception during its execution so method m8() will throw “No Such Method Exception.” As a result, the test will be executed with an exception. As all test cases are passed, this results in a successful test execution.

Actual Result

As there are three test cases in above example, all test cases will be executed one by one. See output below:

See below print statements which can be seen on console: Using @BeforeClass , executed before all test cases Using @Before annotations, executed before each test cases Using @After, executed after each test cases Using @Before annotations, executed before each test cases Using @Test(timeout),it can be used to enforce timeout in JUnit4 test case Using @After, executed after each test cases Using @Before annotations, executed before each test cases Using @Test(expected) ,it will check for specified exception during its execution Using @After, executed after each test cases Using @AfterClass, executed after all test cases

JUnit Assert Class

This class provides a bunch of assertion methods useful in writing a test case. If all assert statements are passed, test results are successful. If any assert statement fails, test results are failed. As you seen earlier, below table describes important Assert methods and description:

JUnit Test Cases Class

To run multiple test, TestCase class is available in org.junit.TestCase packages. @Test annotation tells JUnit that this public void method (Test Case here) to which it is attached can be run as a test case. Below table shows some important methods available in org.junit.TestCase class:

JUnit TestResult Class

When you execute a test, it returns a result (in the form of TestResult object). This TestResult object can be used to analyse the resultant object. This test result can be either failure or successful. See below table for important methods used in org.junit.TestResult class:

JUnit Test Suite Class

If you want to execute multiple tests in a specified order, it can be done by combining all the tests in one place. This place is called as the test suites. See below table for important methods used in org.junit.TestSuite class:

Summary:

JUnit provides a portable API, which provides all important classes and Selenium annotations useful in writing a unit test. Classes which are very useful while writing a test case

org.junit.Assert org.junit.TestCase org.junit.TestResult org.junit.TestSuite

Important and frequently used JUnit annotations list@Before@BeforeClass@After @AfterClass @Test @Ignore

@Test @Ignore