12

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;

...
}
Stephen Isienyi
  • 1,292
  • 3
  • 17
  • 29
  • What is not so useful? Judging from the stack trace you are using a non-compatible version of JUnit 4 with Spring Test. – M. Deinum May 04 '15 at 17:42
  • 1
    I was using JUnit 4.7. I have now gone according to your recommendation and upgraded it to the latest JUnit version 4.12. The error has stopped occurring. That error message is cryptic though and therefore not very useful. But I suppose there is a good reason behind the vagueness. – Stephen Isienyi May 04 '15 at 19:17

1 Answers1

24

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)

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • Hi, I have upgraded Junit version to 4.9 in my project, but still, I am getting the same error. – Pruthviraj Apr 27 '17 at 13:29
  • If you're getting a message from Spring that JUnit 4.9 is required, then you have not successfully upgraded your project to use JUnit 4.9. For example, you may be pulling in different versions of JUnit as transitive dependencies. So verify how JUnit is pulled into your dependency graph. – Sam Brannen Apr 27 '17 at 13:38