Java – InitilizationError when JunitTest with Maven

eclipse-pluginjavajunitmaven

I'm trying to do some Junit-Test's with Maven3 but always get some initilizationError(some testclass here) when executing test which have JUnit-Annotations (all other tests run fine). When I run the tests in Eclipse all is fine. But running the tests with maven will break.
My Testproject is packaged as eclipse-plugin.
I use the maven-osgi-test-plugin.

My testclass is annotated by:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
@TransactionConfiguration
public class ArtifactManagerTest extends
    AbstractTransactionalJUnit4SpringContextTests {...

For every testclass annotated with @ContextConfiguration there is the classname-test.xml file. In this configuration-file I import the applicationContext-Test.xml by "classpath:"

My pom.xml of the test-plugin looks like:

<groupId>foo.bar</groupId>
<artifactId>company.xyz</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>eclipse-test-plugin</packaging>

<build>
 <plugins>
  <plugin>
   <groupId>org.sonatype.tycho</groupId>
   <artifactId>maven-osgi-test-plugin</artifactId>
   <configuration>
    <useUIHarness>true</useUIHarness>
    <testSuite>company.xyz.test</testSuite>
    <testClass>company.xyz.AllTests</testClass>
    <argLine>-Xmx512m</argLine>
    <bundleStartLevel>
     <bundle>
      <id>org.eclipse.equinox.ds</id>
      <level>1</level>
      <autoStart>true</autoStart>
     </bundle>
    </bundleStartLevel>
   </configuration>
   <inherited>true</inherited>
  </plugin>
 </plugins>
</build>

The failure output is as follows:

initializationError(company.xyz.ArtifactManagerTest)  Time elapsed: 0 sec  (ParentRunner.java:66)
        at org.junit.runners.BlockJUnit4ClassRunner.(BlockJUnit4ClassRunner.java:59)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.(SpringJUnit4ClassRunner.java:104)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
        at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java
:29)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
        at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:93)
        at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
        at org.junit.runners.Suite.(Suite.java:101)
        at org.junit.runners.Suite.(Suite.java:67)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:35)
        at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java
:29)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
        at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:93)
        at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
        at org.junit.runners.Suite.(Suite.java:101)
        at org.junit.runners.Suite.(Suite.java:67)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:35)
        at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java
:29)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
        at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.(JUnit4TestSet.java:45)
        at org.apache.maven.surefire.junit4.JUnit4DirectoryTestSuite.createTestSet(JUnit4DirectoryTestSuite.java:56)
        at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.locateTestSets(AbstractDirectoryTestSuite.java:96)
        at org.apache.maven.surefire.Surefire.createSuiteFromDefinition(Surefire.java:209)
        at org.apache.maven.surefire.Surefire.run(Surefire.java:156)
        at org.codehaus.tycho.surefire.osgibooter.OsgiSurefireBooter.run(OsgiSurefireBooter.java:75)
        at org.codehaus.tycho.surefire.osgibooter.AbstractUITestApplication$1.run(AbstractUITestApplication.java:25)
        at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
        at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:134)
        at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4041)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3660)
        at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
        at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
        at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
        at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
        at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
        at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
        at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
        at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
        at org.codehaus.tycho.surefire.osgibooter.UITestApplication.runApplication(UITestApplication.java:21)
        at org.codehaus.tycho.surefire.osgibooter.AbstractUITestApplication.run(AbstractUITestApplication.java:109)
        at org.codehaus.tycho.surefire.osgibooter.UITestApplication.start(UITestApplication.java:27)
        at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
        at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
        at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
        at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
        at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
        at org.eclipse.equinox.launcher.Main.main(Main.java:1383)

In the surefire-reports I see the java.class.path is set to my local maven repository to 'org.eclipse.equinox.launcher-1.1.0.v20100507.jar'

It looks like the classpath isn't set correct, so maven can't find the applicationContext.xml? But how to do this correct?

Best Answer

Please make sure that methods of @BeforeClass and @AfterClass are "public static void" and no arguments in your test case class ArtifactManagerTest.

Edit: the InitializationError is thrown when validate @BeforeClass and @AfterClass fails, so @BeforeClass isn't even executed.

If possible, use following code to get failure info:

Result r=new JUnitCore().run(ArtifactManagerTest.class); 
for(Failure f:r.getFailures()){ 
    System.out.println(f); 
}