Why the cryptic MultipleFailureException error message with the SpringJUnit4ClassRunner.withAfterClasses method

Stephen Isienyi picture Stephen Isienyi · May 4, 2015 · Viewed 11.1k times · Source

Why is my spring test set up failing with the following not-so-useful error messages below? All suggestions are appreciated.

JUnit Output

java.lang.NoClassDefFoundError: org/junit/runners/model/MultipleFailureException
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.withAfterClasses(SpringJUnit4ClassRunner.java:188)
at org.junit.runners.ParentRunner.classBlock(ParentRunner.java:145)
at org.junit.runners.ParentRunner.run(ParentRunner.java:235)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run (SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.junit.runners.model.MultipleFailureException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 10 more

Console Output

INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Could not instantiate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute] INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Could not instantiate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource] INFO : org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@76959acc, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@57e603e6, org.springframework.test.context.support.DirtiesContextTestExecutionListener@3e0a1e1f]

Here is the target snippet

@Service 

public class PipApps {

@Resource( name = "apps" )
private Properties apps;

@Autowired
private SitePreferenceHandler sitePreferenceHandler;

@Autowired
private PipsTable pipsTable;

private SitePreference sitePreference;

private Device device;  

public PipApps( HttpServletRequest request, HttpServletResponse response ){
    sitePreference = sitePreferenceHandler.handleSitePreference( request, response );
    device = DeviceUtils.getRequiredCurrentDevice( request );
}

public Properties getApps(){
    return apps;
}

public Device getDevice(){
    return device;
}

public SitePreference getSitePreference(){
    return sitePreference;
}

public DeviceRouteTable getPipsTable(){
    return pipsTable;
}
}

And the test snippet

@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( locations={"src/test/resources/PipAppsTest-context.xml"} )

public class PipAppsTest {

@Mock
SitePreferenceHandler sitePreferenceHandler;

@Autowired
PipApps pipApps;

...
}

Answer

Sam Brannen picture Sam Brannen · May 5, 2015

Update -- September 2015

Spring Framework 4.2.2 throws a more meaningful exception if JUnit 4.9 is not in the classpath. See SPR-13521 for details.


The following is an excerpt from the class-level Javadoc for SpringJUnit4ClassRunner:

NOTE: As of Spring Framework 4.1, this class requires JUnit 4.9 or higher.

The class in question, MultipleFailureException, was introduced in JUnit 4.9.

So that's why your test is failing with the ClassNotFoundException.

Upgrading to JUnit 4.9 (or preferably 4.12) will therefore solve your problem.

Regards,

Sam (author of the Spring TestContext Framework)