Home >> JUnit

Timeout Test in JUnit

Timeout Test
The “Timeout Test” means if a test method is taking longer time than the specified number of time (in milliseconds) to execute, then the test method will be terminated and marked as failed.

While running test methods there can be cases where certain test methods get struck or may take longer time than to complete the execution than the expected. We need to handle these type of cases by specifying Timeout and proceed to execute further test cases / methods


Exception Test in JUnit

In JUnit we use "expected" with @Test annotation and specify the type of exceptions that are expected to be thrown when executing the test methods.

A below example which throws an exception called “ArithmeticException” when dividing two numbers with denominator value as 0.

	public void dividedByZeroExample1(){
		int e = 1/0;

If we remove the “(expected = ArithmeticException.class)”, it will return exception called “java.lang.ArithmeticException: / by zero”

How to create Test Suite in JUnit

What is Test Suite?

A Test Suite is a grouping unit test cases together and running it. In JUnit, both @RunWith and @Suite annotations are used to run the suite test.

We will show you how to create and run a Suite. We will create two test classes TestOne and TestTwo.

First create a program with class "TestOne" as below:

import org.junit.Test;
public class TestOne {
	public void testcaseOne(){
		System.out.println("Hello im in test case one");

Now Create another program with class "TestTwo" as below:

JUnit Introduction with Example

JUnit is a unit testing framework for the Java programming language and it became important in the development of test-driven development. Annotations - Given by Apache Foundation.

JUnit promotes the idea of testing for developers which emphasis on setting up the test data for a piece of code which can be tested first and can be implemented.

Features of JUnit