2011-01-25 67 views
4

我正在嘗試用Maven3做一些Junit-Test,但在執行有JUnit-Annotations(所有其他測試運行正常)的測試時總是會得到一些initilizationError(某些測試類)。當我在Eclipse中運行測試時,一切正常。但使用maven運行測試將會中斷。 我的Testproject打包爲eclipse-plugin。 我使用maven-osgi-test-plugin。初始化錯誤當JunitTest與Maven

我的TestClass被註釋:

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

對於@ContextConfiguration註解每TestClass中存在的類名-的test.xml文件。

我的測試插件的pom.xml的樣子:

<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> 

失敗輸出如下:

:在此配置文件我用「類路徑」導入的applicationContext-的test.xml
 
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) 

在萬無一失的報告我看java.class.path設置爲我的本地倉庫到「org.eclipse.equinox.launcher-1.1.0.v20100507.jar」

它看起來像班上路徑沒有設置正確,所以maven找不到applicationContext.xml?但如何做到這一點是正確的?

回答

1

請確保的@BeforeClass@AfterClass方法「公共靜態無效的」,並在你的測試用例類ArtifactManagerTest沒有參數

編輯:當驗證@BeforeClass@AfterClass失敗時將引發InitializationError,因此@BeforeClass甚至沒有執行。

如果可能的話,使用下面的代碼獲取故障信息:

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

是的,他們是靜態的,沒有任何參數。問題是這些方法甚至不會執行。 initilizationError發生在之前。 – Sid 2011-01-25 14:13:53