JUnit - SPLessons

JUnit Test Exception

Chapter 8

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JUnit Test Exception

JUnit Test Exception

shape Introduction

This chapter demonstrates Junit Test Exception which is created by creating a Java class in order to do this arrange the expected field of class equal to an exception class then which throws the Junit Test Exception. Following are the topics are covered in this chapter.

  • Test Methods
  • Time Out

Test Methods

shape Description

Junit Test Exception are having the different Test methods which defines a specific end goal to make unit tests In JUnit, client needs to make test methods. Which method define that is public and has a void return type. User can add the JUnit test annotations to those test methods.

In previous versions of JUnit test method name start with the Test. Then only JUnit runner recognize the test and it will be picked up to the test. But now in new versions of JUnit the name should be anything.

If the user gave different names it is a possibility to see the results, and user can identify what test was trying to test. More over user can easily identify which test is running successful and which test is broken. And also it is a possible way to group the test methods that test a specific class in a class and user can apply certain behaviour to a group of test methods.

In order to do the test JUnit has the many more test methods in following are the some of the test methods in JUnit Test Exception.

Test Timeout

shape Description

In JUnit Test Exception Some times user need to measure the test performance in terms of a time. for this JUnit has a parameter named has “time out”. If test take longer time to instead of given time then automatically test will be failed.

shape Step 1

In the time out test method programmer will give time to the test method. Whatever the time given by the programmer up to that time only test will be alive later it should be failed. Following code will demonstrate about the Time out method.

package com.simpleprogrammer.proteintracker;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.After;
import org.junit.AfterClass;
import com.simpleprogram.proteintracker.InvalidException;
import com.simpleprogram.proteintracker.Notifier;
import com.simpleprogram.proteintracker.TrackingService;
import static org.junit.Assert.*;

public class trackingServiceeTest {
	private TrackingService service;
	public static void before()
		System.out.println("Before Class");
	public static void after()
		System.out.println("After Class");
	public void setUp()
		service = new TrackingService();
	public void tearDown()
	@Test(timeout = 200)
	public void BadTest()
		for(int i=0; i>10000000; i++)

the below image demonstrate the test time out method in JUnit Test Exceptions while executing the code in the given time only it will be positive which means green colour after the timed out it will change to red colour as shown in below image.

shape Step 2

If the program will be timed out it should moved to the test failed state that should be shown below image.

What ever the time given by the user up to that time it will be in test pass state if the time out it will moved to failed state by using this user can create the time intervals to perform the tests.


shape Key Points

  • By using Time out user can create time intervals in Junit Test Exception.
  • Each and every test method has its own priority.
  • Test time out shown in red color.