About Vinod Kumar Kashyap

Vinod is Sun Certified and love to work in Java and related technologies. Having more than 10 years of experience, he had developed software's including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS. He is also a JUG Leader of Chandigarh Java User Group.

JUnit BlockJUnit4ClassRunner Example

In this tutorial we shall show users the JUnit BlockJUnit4ClassRunner Example. We will analyze and see when and why should we use the JUnit Blockjunit4classrunner. Also we will learn about the different runners that we sued in the JUnit. But in this tutorial we will have our main focus on JUnit Blockjunit4classrunner class.

1. Introduction

JUnit provides different type of runners that help us to test out cases. JUnit Blockjunit4classrunner is the default class that will run if we do not provide any other type of runner to the test class. For example:

Here we have not defined any runner class. So, by default Blockjunit4classrunner will be used to run the test cases.

Tip
Class BlockJUnit4ClassRunner is the default Runner that is used if no other Runner is provided

As you are surprised where we will be using the runner, then do not panic as we will also show it here. We have to use the runner with the @RunWith annotation as a parameter to it. One of the common runner that we use is the Suite.class.
Let’s see the Suite example.

Here we are using the Suite.class to run out test cases. More detailed example of the Suite class can be found at JUnit Group Tests Example

2. JUnit BlockJUnit4ClassRunner

So, what is the use of BlockJUnit4ClassRunner class? It is the default class that is called whenever we run any test case. Before BlockJUnit4ClassRunner class we have a JUnit4ClassRunner class that is by default called. But now as of latest JUnit this class has been depreciated and instead we use BlockJUnit4ClassRunner class.

See below details about the class dependencies hierarchy.

Figure 1: JUnit BlockJUnit4ClassRunner Class Architecture

In this figure boxes, marked orange are interfaces and blue are classes. It is clear that BlockJUnit4ClassRunner class extends the ParentRunner class and obviously implement some of the methods of it. We will see some methods below.

There are lot of methods in this class. But we will see the common methods that are useful and understand about the class.
1. List getChildren(): This will scan the class for the methods annotated with @Test annotation.
2. Description describeChild(FrameworkMethod method): This will create a description on the basis of the method name
3. void runChild(final FrameworkMethod method, RunNotifier notifier): This will run all methods except the methods with @Ignore annotation. This will run @Before, then @Test and after that @After.

In this file line numbers 1-8 show the dependencies that are required to run our project. We have added the JUnit 4.12 for in our project. Lines 9-23 show the build properties of the project.Line number 10 indicates that the final name after building of our project will be “junitrunner“.Line 18-19 tell that we are using Java 8 for building and compiling the project.

5.1 Test Classes

We will first see the example where we do not have to define the runner i.e. by default BlockJUnit4ClassRunner class is used.

7. Conclusion

In this example we have seen what is BlockJUnit4ClassRunner class and why it is important in JUnit. There are by default 2 classes that we use in JUnit. One is BlockJUnit4ClassRunner class and the other is Suite class. We have seen that we can extend the class and create custom runners according to our own preference.

8. Download the Eclipse Project

This is a JUnit BlockJUnit4ClassRunner Example.

Download
You can download the full source code of this example here: junitrunner.zip

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.