Junit test runner

Test runner is used for executing the test cases. Example: DivisionTestCase.java import com.w3spoint.business.*; import static org.junit.Assert.*; import org.junit.Test;   /** * This is test case class for division method. * @author w3spoint */ public class DivisionTestCase { //DivisionTest class objects DivisionTest divisionTest1 = new DivisionTest(10, 2); DivisionTest divisionTest2 = new DivisionTest(10, 0);   //Test case … Read more

JUnit parameterized test

The parameterized test is a new feature introduced in JUnit 4. It provides the facility to execute the same test case again and again with different values. Steps to create a parameterized test: 1. Test class have to be annotated with @RunWith(Parameterized.class) annotation. 2. Create a public static method with @Parameters annotation which returns a … Read more

JUnit suite test

The suite test refers to group a few unit test cases and run it together. The @RunWith and @Suite annotation are used to run the suite test. Example: TestSuite.java import org.junit.runner.RunWith; import org.junit.runners.Suite;   /** * This class is used as suite test class. * Both DivisionTestCase1 and DivisionTestCase2 * executes together after TestSuite is … Read more

JUnit time test

The time test is used to check that if a test case is completed within the specified time or not. JUnit terminate and mark it failed automatically if it takes more time than the specified milliseconds. We have to specify time in milliseconds in @Test(timeout = timeInMilliSeconds) action. Example: DivisionTestCase.java import com.w3spoint.business.*; import static org.junit.Assert.*; … Read more

Junit ignore test

The @Ignore annotation is used to ignore test cases. The @Ignore annotation can be used with method or class. If it is used with the method then that method will be ignored by JUnit and will not execute and if it is used with the class then all methods of that class will be ignored … Read more

JUnit expected exception test

Expected exception test is used for the methods which can throw an exception. We have to specify expected exception in @Test(expected = expectedException.class) action. If expected exception or any of its subclass exception is thrown by the method then JUnit will pass this unit test. Example: DivisionTestCase.java import com.w3spoint.business.*; import static org.junit.Assert.*; import org.junit.Test;   … Read more

Junit basic annotation example

Annotations for Junit testing: 1. @Test: It is used to specify the test method. 2. @BeforeClass: It is used to specify that method will be called only once, before starting all the test cases. 3. @AfterClass: It is used to specify that method will be called only once, after finishing all the test cases. 4. … Read more