Pragmatic Programmer Issues

RunWith helps JUnit

Comments: 4

When we want to test with Junit4 we can spot some problems. Firstly I thought that this is a maven-surefire-plugin problem, but as I dig into, the problem shows in JUnit runner. We track this problem on simple example.

We create simple project.

$ mvn archetype:generate

We move to the project and we run test – everything is ok.

$ mvn test

Now we change JUnit version in pom.xml from 3.8.1 to 4.4. We run test again and still everything is ok.
We continue by adding java 5 target compilation.


Next we change testApp() method to something like this (JUnit4)

     * Rigorous Test :-)
    public void tapp()   {
        assertTrue( true );

Test fails !!!. The stacktrace is :

Test set: info.pietrowski.AppTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.036 sec <<< FAILURE!
warning(junit.framework.TestSuite$1)  Time elapsed: 0.008 sec  <<< FAILURE!
junit.framework.AssertionFailedError: No tests found in info.pietrowski.AppTest
	at junit.framework.TestSuite$1.runTest(
	at junit.framework.TestCase.runBare(
	at junit.framework.TestResult$1.protect(
	at junit.framework.TestResult.runProtected(
	at junit.framework.TestSuite.runTest(
	at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(
	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(
	at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(
	at org.apache.maven.surefire.booter.SurefireBooter.main(

No tests found !!!. What’s going on. The core thread-dump lines are:

 at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(

This means that maven-surefire-plugin run Junit4TestSet as it should do, but unfortunately JUnit runs Junit38ClassRunner !!!. This strange behavior is due we still extends TestCase class. So the quickies solution is to drop this extend. We can not use now assert* methods, the solution is to static import org.junit.Assert.*. Everything backs to normal.

But what if we want to extend TestCase (DBUnit, Spring*TestCase). In case of Spring test classes there is special tree for JUnit4 and TestNG so every TestCase class has equivalent in JUnit4 and TestNG. So for example AbstractTransactionalJUnit38SpringContextTests, has equivalents AbstractTransactionalJUnit4SpringContextTests, and AbstractTransactionalTestNGSpringContextTests.

The problem still exists when library does not provide such equivalents. Let’s look to the code, the problematic function is

The procedure is simple, if class has @Ignore annotation than we skip this class, next we look if class has @RunWith annotation if so we get value of this annotation (we talk about it later). Next three is simple if class has suite method we run Suite runner called AllTest, if class is subclass of TestCase then we use JUnit38ClassRunner if everything else we run with JUnit4ClassRunner. We solved the mystery 😉

Our only hope is @RunWith annotation, we can use it with JUnit4ClassRunner and than everything is alright.

Of course we can use this annotation with any class that extends Runner. I hope this solves your problems. See you.





Thanks for info about BlockJUnit4Runner. And about my proposition, its OK for JUnit 4.x users but not 3.x users so I treated it as workaround not solution.

perhaps in JUnit 4.5 the JUnit4ClassRunner is deprecated. Use
BlockJUnit4Runner instead

Hi Pedro, thanx a lot! This workaround did it!

Why do not you submit your proposition to


Anna SkawiƄskaAnna SkawiƄska

Thanks a lot! How could I have not noticed that spring-test classes extend Junit TestCase…