我有幾個Eclipse插件項目,我想使用Maven & Tycho進行編譯。該項目可在github找到。在那裏你看到三個項目與他們的pom.xml
文件。我的目標是使用JMockit來運行單元測試。與Tycho一起使用JMockit
要啓動Maven的,我切換到maven.parent
目錄並執行mvn clean verify
產生以下輸出:
D:\maven-test\maventest.parent>mvn clean verify
[INFO] Scanning for projects...
[INFO] Computing target platform for MavenProject: maventest:maventest:1.0.0-SNAPSHOT @ D:\maven-test\maventest\pom.xml
[INFO] Adding repository ....censored....
[INFO] Adding repository ....censored....
[INFO] Resolving dependencies of MavenProject: maventest:maventest:1.0.0-SNAPSHOT @ D:\maven-test\maventest\pom.xml
[INFO] Resolving class path of MavenProject: maventest:maventest:1.0.0-SNAPSHOT @ D:\maven-test\maventest\pom.xml
[INFO] Computing target platform for MavenProject: maventest:test.maventest:1.0.0-SNAPSHOT @ D:\maven-test\test.maventest\pom.xml
[INFO] Resolving dependencies of MavenProject: maventest:test.maventest:1.0.0-SNAPSHOT @ D:\maven-test\test.maventest\pom.xml
[INFO] Resolving class path of MavenProject: maventest:test.maventest:1.0.0-SNAPSHOT @ D:\maven-test\test.maventest\pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] maventest.parent
[INFO] maventest
[INFO] test.maventest
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maventest.parent 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maventest.parent ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building maventest 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maventest ---
[INFO]
[INFO] --- tycho-packaging-plugin:0.24.0:build-qualifier (default-build-qualifier) @ maventest ---
[INFO] The project's OSGi version is 1.0.0.201512010744
[INFO]
[INFO] --- tycho-packaging-plugin:0.24.0:validate-id (default-validate-id) @ maventest ---
[INFO]
[INFO] --- tycho-packaging-plugin:0.24.0:validate-version (default-validate-version) @ maventest ---
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ maventest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven-test\maventest\src\main\resources
[INFO]
[INFO] --- tycho-compiler-plugin:0.24.0:compile (default-compile) @ maventest ---
[INFO] Compiling 2 source files to D:\maven-test\maventest\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ maventest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven-test\maventest\src\test\resources
[INFO]
[INFO] --- target-platform-configuration:0.24.0:target-platform (default-target-platform) @ maventest ---
[INFO]
[INFO] --- tycho-packaging-plugin:0.24.0:package-plugin (default-package-plugin) @ maventest ---
[INFO] Building jar: D:\maven-test\maventest\target\maventest-1.0.0-SNAPSHOT.jar
[INFO]
[INFO] --- tycho-p2-plugin:0.24.0:p2-metadata-default (default-p2-metadata-default) @ maventest ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building test.maventest 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ test.maventest ---
[INFO]
[INFO] --- tycho-packaging-plugin:0.24.0:build-qualifier (default-build-qualifier) @ test.maventest ---
[INFO] The project's OSGi version is 1.0.0.201512010744
[INFO]
[INFO] --- tycho-packaging-plugin:0.24.0:validate-id (default-validate-id) @ test.maventest ---
[INFO]
[INFO] --- tycho-packaging-plugin:0.24.0:validate-version (default-validate-version) @ test.maventest ---
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ test.maventest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven-test\test.maventest\src\main\resources
[INFO]
[INFO] --- tycho-compiler-plugin:0.24.0:compile (default-compile) @ test.maventest ---
[INFO] Compiling 1 source file to D:\maven-test\test.maventest\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.4.3:testResources (default-testResources) @ test.maventest ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\maven-test\test.maventest\src\test\resources
[INFO]
[INFO] --- target-platform-configuration:0.24.0:target-platform (default-target-platform) @ test.maventest ---
[INFO]
[INFO] --- tycho-packaging-plugin:0.24.0:package-plugin (default-package-plugin) @ test.maventest ---
[INFO] Building jar: D:\maven-test\test.maventest\target\test.maventest-1.0.0-SNAPSHOT.jar
[INFO]
[INFO] --- tycho-p2-plugin:0.24.0:p2-metadata-default (default-p2-metadata-default) @ test.maventest ---
[INFO]
[INFO] --- tycho-surefire-plugin:0.24.0:test (default-test) @ test.maventest ---
[INFO] Expected eclipse log file: D:\maven-test\test.maventest\target\work\data\.metadata\.log
[INFO] Command line:
[C:\Program Files\Java\jdk8\jre\bin\java.exe, -Dosgi.noShutdown=false, -Dosgi.os=win32, -Dosgi.ws=win32, -Dosgi.arch=x86_64, -javaagent:D:\maven-test\org.jmockit_1.20.0.jar, -Dosgi.clean=true,
-jar, C:\Users\cludwig\.m2\repository\p2\osgi\bundle\org.eclipse.equinox.launcher\1.3.100.v20150511-1540\org.eclipse.equinox.launcher-1.3.100.v20150511-1540.jar, -data, D:\maven-test\test.maventest\t
arget\work\data, -install, D:\maven-test\test.maventest\target\work, -configuration, D:\maven-test\test.maventest\target\work\configuration, -application, org.eclipse.tycho.surefire.osgibooter.headles
stest, -testproperties, D:\maven-test\test.maventest\target\surefire.properties]
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running test.maventest.Foo_UnitTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.118 sec <<< FAILURE! - in test.maventest.Foo_UnitTest
initializationError(test.maventest.Foo_UnitTest) Time elapsed: 0.002 sec <<< ERROR!
java.lang.VerifyError: null
at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
at sun.instrument.InstrumentationImpl.redefineClasses(InstrumentationImpl.java:170)
at mockit.internal.startup.Startup.redefineMethods(Startup.java:242)
at mockit.internal.startup.Startup.redefineMethods(Startup.java:236)
at mockit.internal.mockups.MockClassSetup.applyClassModifications(MockClassSetup.java:134)
at mockit.internal.mockups.MockClassSetup.redefineMethodsInClassHierarchy(MockClassSetup.java:91)
at mockit.internal.mockups.MockClassSetup.redefineMethods(MockClassSetup.java:76)
at mockit.MockUp.redefineMethods(MockUp.java:239)
at mockit.MockUp.redefineClassOrImplementInterface(MockUp.java:221)
at mockit.MockUp.<init>(MockUp.java:130)
at mockit.integration.junit4.internal.RunNotifierDecorator.<init>(RunNotifierDecorator.java:21)
at mockit.internal.startup.JMockitInitialization.loadInternalStartupMocksForJUnitIntegration(JMockitInitialization.java:35)
at mockit.internal.startup.JMockitInitialization.initialize(JMockitInitialization.java:26)
at mockit.internal.startup.Startup.applyStartupMocks(Startup.java:72)
at mockit.internal.startup.Startup.reapplyStartupMocks(Startup.java:124)
at mockit.internal.startup.Startup.getInstrumentation(Startup.java:188)
at mockit.internal.startup.Startup.initializeIfPossible(Startup.java:198)
at mockit.integration.junit4.JMockit.<clinit>(JMockit.java:21)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:262)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:156)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:82)
at org.eclipse.tycho.surefire.osgibooter.OsgiSurefireBooter.run(OsgiSurefireBooter.java:91)
at org.eclipse.tycho.surefire.osgibooter.HeadlessTestApplication.run(HeadlessTestApplication.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.equinox.internal.app.EclipseAppContainer.callMethodWithException(EclipseAppContainer.java:587)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:198)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
Results :
Tests in error:
Foo_UnitTest.initializationError ╗ Verify
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] maventest.parent ................................... SUCCESS [ 0.219 s]
[INFO] maventest .......................................... SUCCESS [ 4.227 s]
[INFO] test.maventest ..................................... FAILURE [ 4.320 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.972 s
[INFO] Finished at: 2015-12-01T08:44:14+01:00
[INFO] Final Memory: 45M/405M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.eclipse.tycho:tycho-surefire-plugin:0.24.0:test (default-test) on project test.maventest: There are test failures.
[ERROR]
[ERROR] Please refer to D:\maven-test\test.maventest\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR]
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR] mvn <goals> -rf :test.maventest
如果我的父母聚甲醛去除-javaagent
,然後我得到另一個異常:
Running test.maventest.Foo_UnitTest
JMockit: Reinitializing under custom class loader [email protected][org.jmockit:1.20.0(id=87)]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:386)
at sun.instrument.InstrumentationImpl.loadClassAndCallAgentmain(InstrumentationImpl.java:411)
Caused by: java.lang.VerifyError
at sun.instrument.InstrumentationImpl.redefineClasses0(Native Method)
at sun.instrument.InstrumentationImpl.redefineClasses(InstrumentationImpl.java:170)
at mockit.internal.startup.Startup.redefineMethods(Startup.java:242)
at mockit.internal.startup.Startup.redefineMethods(Startup.java:236)
at mockit.internal.mockups.MockClassSetup.applyClassModifications(MockClassSetup.java:134)
at mockit.internal.mockups.MockClassSetup.redefineMethodsInClassHierarchy(MockClassSetup.java:91)
at mockit.internal.mockups.MockClassSetup.redefineMethods(MockClassSetup.java:76)
at mockit.MockUp.redefineMethods(MockUp.java:239)
at mockit.MockUp.redefineClassOrImplementInterface(MockUp.java:221)
at mockit.MockUp.<init>(MockUp.java:130)
at mockit.integration.junit4.internal.RunNotifierDecorator.<init>(RunNotifierDecorator.java:21)
at mockit.internal.startup.JMockitInitialization.loadInternalStartupMocksForJUnitIntegration(JMockitInitialization.java:35)
at mockit.internal.startup.JMockitInitialization.initialize(JMockitInitialization.java:26)
at mockit.internal.startup.Startup.applyStartupMocks(Startup.java:72)
at mockit.internal.startup.Startup.reapplyStartupMocks(Startup.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at mockit.internal.util.MethodReflection.invoke(MethodReflection.java:94)
at mockit.internal.util.MethodReflection.invoke(MethodReflection.java:159)
at mockit.internal.startup.Startup.reinitializeJMockitUnderCustomClassLoader(Startup.java:118)
at mockit.internal.startup.Startup.agentmain(Startup.java:101)
... 6 more
java.lang.IllegalStateException: com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:262)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:156)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:82)
at org.eclipse.tycho.surefire.osgibooter.OsgiSurefireBooter.run(OsgiSurefireBooter.java:91)
at org.eclipse.tycho.surefire.osgibooter.HeadlessTestApplication.run(HeadlessTestApplication.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.equinox.internal.app.EclipseAppContainer.callMethodWithException(EclipseAppContainer.java:587)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:198)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
Caused by: com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize
... 26 more
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.382 sec <<< FAILURE! - in test.maventest.Foo_UnitTest
test_value(test.maventest.Foo_UnitTest) Time elapsed: 0.045 sec <<< ERROR!
java.lang.IllegalStateException: Invalid place to record expectations
at test.maventest.Foo_UnitTest$1.<init>(Foo_UnitTest.java:33)
at test.maventest.Foo_UnitTest.getFoo(Foo_UnitTest.java:33)
at test.maventest.Foo_UnitTest.test_value(Foo_UnitTest.java:25)
Results :
Tests in error:
Foo_UnitTest.test_value:25->getFoo:33 ╗ IllegalState Invalid place to record e...
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
Agent failed to start!
Exception in thread "Attach Listener"
所以我得出結論-javaagent
是某種程度上需要的。令人困惑的是:使用eclipse來運行測試時,它就像一個魅力!
傳遞給-javaagent
因此用於運行測試的jmockit-1.20.0.jar
與eclipse存儲庫中的完全相同。我還添加了@RunWith(JMockit.class)
進行測試。如here所述,我使用了所有三種方法。
那麼你有什麼線索我可以做什麼?
嘗試爲Java 7添加'-noverify'(對於Java 8)或'-XX:-UseSplitVerifier'來關閉拆分驗證器(Java選項 - 可能需要將傳遞到您的測試插件中) –
Jan
@Jan在執行此操作時,會發生另一個與[link](http://stackoverflow.com/q/32994290/2318387)中發佈相同的錯誤。 – Christoph
<! - http://stackoverflow.com/a/4369038 - > \t \t \t \t \t -javaagent: 「d:\行家測試\ org.jmockit_1.20.0.jar」 -noverify -XX: -UseSplitVerifier 那你是怎麼做到的? –
Jan