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
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.
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
Description
In order to run multiple classes user need to create the Suite class. The below programs demonstrate creating a suite class.
[c]mport org.junit.runner.RunWith;
import org.junit.runners.Suits;
RunWIth(Suite.class)
@Suite.SuiteClasses({
HelloJUnitTest.class,
TrackingServiceTests.class
})
public class ProteinTrackerSuits {
}[/c]
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
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.
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.
[c]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 {
}[/c]
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
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.
Org.junit.runner
JUnitCore
Junit38ClassRunner
Junit4ClassRunner
Parameterized
Conceptual
figure
The below image demonstrate the Class hierarchy of standard JUnit Runner.
The method returns a description displaying a runner.
getDescription
public abstract Description getDescription()
A test which to be return by the receiver.
Run
Public abstract void run(RunNotifier notifier)
The method notify the events while tests are being run and also notify either the test being started, finished and failed.
testCount
public int testCount()
The method returns the no of tests that are been to run by the receiver.
I order to get the JUnit runner visit the official page of JUnit.org for JUnit runner methods as shown in below image.
Examples
The code below demonstrate that console runner class. By implementing runner method.
[c]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);
}
}[/c]
The console runner class display the no of failure test and successful tests in the below image.
JUnit Jars
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.
Step 1
In order to download JUnit JARs click the official page of JUnit.org as shown in below image.
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.
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
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.