JUnit - SPLessons

JUnit Test Class

Chapter 7

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JUnit Test Class

JUnit Test Class

shape Introduction

This chapter demonstrate the JUnit Test Classes. Which explore the some features of the test cases those features are use full to perform more test cases at a single time. JUnit have the several test classes below demonstrate some of the Junit Test Class.

  • Creating Test class in JUnit.
  • Parameterized test

Creating Test class in JUnit

shape Description

In order to create the Junit Test Class which has the several basic test methods based on the test requirement. JUnit runs the multiple test methods and some annotations at a time the below code demonstrates the creating a Junit Test Class.

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.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()

	public void NewTrackingServiceTotalIsZero()
		assertEquals("Tracking service was not zero", 0, service.getTotal());

	public void WhenAddingProteinTotalIncresesByThatAmount()
		assertEquals("Protein amount was not correct", 10, service.getTotal());
	public void WhenRemovingProteinTotalRemainsZero()
		assertEquals(0, service.getTotal());

If user run the above generated code it will run all mentioned methods and annotations successfully and which displays the output in green colour as shown in below image.

Parameterized Tests

shape Description

Parameterized Tests used to pass a set of inputs and expected outputs into a test class and runs against those inputs and check to the expected results without creating a test case for each pair. In order to create simple parameterized JUnit test user need to create static method with parameters annotation which returns a collection of arrays that have one item for input and another one is for expected output. Below image demonstrate the Parameterized test.

Creating Parameterized Tests

Following are the steps to create Parameterized Tests.

shape Examples

The below code demonstrate to create a test to specify the series of input for Protein that either going to add or remove.

import java.utill.Arrays;
import java.utill.List;
import org.junit.runner.Runwith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import com.simpleprogrammer.proteintracker.Trackingservice;

public class PrametrizecTests {
	Private static Tracking Service=new TrackingService();
	public static List<Object[]> data(){
		return Arrays.osList(new Object[][]){
			{5, 5},
			{5, 10},
			{-12, 0},
			{50, 50},
			{1, 51}
	public ParametrizedTests(int input, int expected){
		this.input = input;
		this.expected = expected;
	public voi tst(){
		if(input <=0)

The output for the above program is shown below.


shape Key Points

  • Junit Test Class – User can pass set of inputs to Parameterized test.
  • While creating tests follow the naming conventions.
  • Output shows the executed procedure by using annotations.
  • Parameterized tests will run against the expected outputs.