JUnit - SPLessons

JUnit Test Suites

Chapter 11

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

JUnit Test Suites

JUnit Test Suites

shape Introduction

This chapter demonstrate the JUnit Test Suits and JUnit Runners, there are several ways to perform the testing on Java projects. Different classes Combine together to form a Test Suits. Following are the topics.

  • Test Suites
  • Runners Of JUnit
  • JUnit Jars

Test Suites

shape Description

In order to run multiple classes, user need to select the every individual class and run individually. But in the case of JUnit test user can run multiple classes together by creating Test Suit class. In order to use the JUnit Test Suites user need to annotate with the Run With annotation.

shape Conceptual
figure

The Figure demonstrates the JUnit Test Suites deals with the multiple Test classes and the Test Suites annotated with the @RunWith which is a suite class.

Creating A Suite Class

shape Description

In order to run multiple classes user need to create the Suite class. The below programs demonstrate creating a suite class.

mport org.junit.runner.RunWith;
import org.junit.runners.Suits;

RunWIth(Suite.class)
@Suite.SuiteClasses({
	HelloJUnitTest.class,
	TrackingServiceTests.class
})
public class ProteinTrackerSuits {

}

The above program have two classes HelloJUnitTest.class, TrackingServiceTests.class which can run at a time and execute the output shown in the below image.

Test Suits Categories

shape Description

Categories are similar to JUnit Test Suites in JUnit and behaves like a suite runner, user can add a category to a test class or test method. Basically category able to read the category annotations of the test methods or classes. If both the categories are matched then test will able to run otherwise test fails and also supports Inheritance. User can also create Hierarchies of category.

shape Examples

In order to deal with the Category annotation first user need to create the class. The code below demonstrate creating a class and run the category of the different classes.

import org.junit.exprimental.categories.Catogeries;
import org.junit.exprimental.categories.Catogeries.ExcludingCategory;
import org.junit.exprimental.categories.Catogeries.IncludingCategory;
import org.Junit.runner.RunWith;
import org.Junit.runner.Suits;

@Runwith(Categories.class)
@Includecategory(GoodTestCategory.class)
@Suite.SuiteClasses({
	HelloJUnitTest.Class,
	TrackingServiceTests.Class
})
public class GoodtestSuit {

}

Add Category annotations in another class and run in the created class then it will run the multiple classes then Display the output in positive way.

Runners Of JUnit

shape Description

JUnit runner is class which extend abstract Runner class of JUnit. @RunWith annotation used to run the JUnit class. In the core package JUnit runner provides a series JUnit4 runners as follows.

shape Conceptual
figure

The below image demonstrate the Class hierarchy of standard JUnit Runner.

The method returns a description displaying a runner.


I order to get the JUnit runner visit the official page of JUnit.org for JUnit runner methods as shown in below image.

shape Examples

The code below demonstrate that console runner class. By implementing runner method.

import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;

public class ConsoleRunner {
	public static void main(String arg[]){
		JUnitCore junit = new JUnitCore();
		junit.addListener(new TextListener(system.out));
		
		junit.run(TrackingServiceTests.class);
	}
}

The console runner class display the no of failure test and successful tests in the below image.

JUnit Jars

shape Description

If user not able to run the code using Java from the command line user need to download the JUnit JARs. By downloading JAR files user able to get full command line version of JUnit. The steps below demonstrate the downloading process of JUnit.

shape Step 1

In order to download JUnit JARs click the official page of JUnit.org as shown in below image.

shape Step 2

Now the page redirect to another page provided JUnit.jar and hamcrest.jar files. Click on these files as shown in the below image.

shape Step 3

Now the page again redirect to another page provide some Details of JAR files like Versions, Group id, Dates. Select the appropriate and start downloading as shown in below image.

Summary

shape Key Points

  • JUnit Test Suites – Multiple Test Classes are combined together to form a Test Suites.
  • @RunWith annotation used to run Junit Runner.
  • For JUnit Java projects JUnit JARs are mandatory.
  • Console runner display the all test cases information.