Home » Core Java » junit » JUnit BlockJUnit4ClassRunner Example

About Vinod Kumar Kashyap

Vinod is Sun Certified and love to work in Java and related technologies. Having more than 13 years of experience, he had developed software's including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS, AWS. 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:


package com.javacodegeeks;

import static org.junit.Assert.assertTrue;
import org.junit.Test;

public class TestClass {

    public void testA(){

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

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.


public class TestClass {}

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.

JUnit BlockJUnit4ClassRunner Class Architecture

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.

3. Technologies Used

We are using the following technologies for this example to work.

  • Java 1.8
  • JUnit 4.12
  • Eclipse
  • Maven

4. Project Setup

Let’s start creating our project.

You may skip project creation and jump directly to the beginning of the example below.

Open Eclipse. Click File -> New -> Maven Project Check the first check mark and click on Next button.

JUnit BlockJUnit4ClassRunner Example Setup 1

Figure 2: JUnit BlockJUnit4ClassRunner Example Setup 1

On this screen fill in the details as shown and click on the Finish button.

JUnit BlockJUnit4ClassRunner Example Setup 2

Figure 3: JUnit BlockJUnit4ClassRunner Example Setup 2

With this, we are finished with the creating of the blank Maven project.

5. JUnit BlockJUnit4ClassRunner Example

Before starting the coding part we need to add some lines to the pom.xml of the project. It contains all the jars we need to have for this project including some configurations.



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.

5.1.1 Default Runner


package com.javacodegeeks;

import static org.junit.Assert.*;

import org.junit.Test;

public class DefaultRunnerTest {

    public void testDefault(){

Here we are not using any runner but it will run. The output for the following will be:


Figure 4: output-default

5.1.2 Suite Runner

Lets take an example of Suite runner. We will create 2 classes and then add them to Suite to run both.


package com.javacodegeeks;

import org.junit.Ignore;
import org.junit.Test;

public class ClassATest {

    public void classA_Test1() {

    public void classA_Test2() {


package com.javacodegeeks;

import org.junit.Test;

public class ClassBTest {

    public void classB_Test1() {

    public void classB_Test2() {


package com.javacodegeeks;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@SuiteClasses({ ClassATest.class, ClassBTest.class})
public class SuiteRunnerTest {


When we run this example we will see the following output in console


If you notice one of the test case is not run in ClassATest with the @Ignore annotation. Because it is ignored by the runner.

Output in JUnit window is shown as this:


Figure 5: output-suite

6. Custom Runners

We can also make custom runners for our use. There are many other runners available on the market. Some of the main Test runners that we widely used are:

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.

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments