2011-12-17 101 views
1

我想運行一個測試,由另一個程序員用eclipse-indigo中的JUnit編寫,並且他確定它能正常工作,但是當我嘗試Jmockit時出現故障,並且找不到解決方案,但是。Jmockit NullPointerException與Junit

我嘗試更改類路徑中的jmockit.jar,但無法正常工作。你對這個問題有什麼想法嗎?

編輯:當我在終端中使用「ant test」時沒有問題,但是如果我單擊run>在我的IDE中執行JUnit測試,則出現此錯誤。

Exception in thread "(Attach Listener)" java.lang.reflect.InvocationTargetException 
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 sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:323) 
at sun.instrument.InstrumentationImpl.loadClassAndCallAgentmain(InstrumentationImpl.java:348) 
Caused by: java.lang.NullPointerException 
at mockit.internal.state.TestRun.getInstance(TestRun.java:40) 
at mockit.internal.state.TestRun.mockFixture(TestRun.java:111) 
at mockit.internal.ClassFile.createClassFileReader(ClassFile.java:33) 
at mockit.internal.annotations.AnnotatedMockMethodCollector.collectMockMethods(AnnotatedMockMethodCollector.java:39) 
at mockit.internal.RedefinitionEngine.<init>(RedefinitionEngine.java:86) 
at mockit.internal.RedefinitionEngine.<init>(RedefinitionEngine.java:110) 
at mockit.internal.startup.Startup.setUpInternalStartupMock(Startup.java:130) 
at mockit.internal.startup.Startup.loadInternalStartupMocksForJUnitIntegration(Startup.java:112) 
at mockit.internal.startup.Startup.initialize(Startup.java:79) 
at mockit.internal.startup.Startup.agentmain(Startup.java:69) 
... 6 more 
Agent failed to start! 
[WARN ][load ] agent library 'instrument' failed to init with result: 102 
java.lang.RuntimeException: com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize 
at mockit.internal.startup.JDK6AgentLoader.loadAgentAndDetachFromThisVM(JDK6AgentLoader.java:130) 
at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:80) 
at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:41) 
at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:244) 
at org.junit.runner.Runner.<clinit>(Runner.java:25) 
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13) 
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) 
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:30) 
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) 
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:455) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:684) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:391) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: com.sun.tools.attach.AgentInitializationException: Agent JAR loaded but agent failed to initialize 
at sun.tools.attach.HotSpotVirtualMachine.loadAgent(HotSpotVirtualMachine.java:122) 
at mockit.internal.startup.JDK6AgentLoader.loadAgentAndDetachFromThisVM(JDK6AgentLoader.java:123) 
at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:81) 
... 16 more 
java.lang.NoClassDefFoundError: org/junit/internal/runners/ErrorReportingRunner 
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
+0

您可以發表一些代碼嗎?最好是一些小的東西,仍然會出現錯誤。 – 2011-12-18 21:45:04

+0

實際上,當我在終端中使用「ant test」時沒有問題,但我單擊run>在我的IDE中執行JUnit測試。我得到了這個錯誤。 – afsinka 2011-12-19 10:05:11

+0

也許ant設置了一些額外的東西,它不會在測試的eclipse啓動配置中設置 – oers 2011-12-19 11:48:39

回答

3

正如你在你的評論中所述,你在ant中爲執行設置了一個額外的參數。
嘗試設置

-javaagent:libtest/jmockit.jar 

在您的JUnit測試時,啓動配置虛擬機的參數(其參數選項卡上的第二個輸入字段)。

並確保類路徑與ant中的相同。

enter image description here

+0

非常感謝你@oers,它的工作原理! – afsinka 2011-12-19 13:00:17

0

如果你有發行版的zip文件,只需調試一下,看看它爲什麼得到NPE就很容易。只需將源附件添加爲zip文件即可。

+0

謝謝你的回答,但實際上當我在終端中使用「ant test」時沒有任何問題,但我單擊run>在我的IDE中執行JUnit測試我得到了這個錯誤。 – afsinka 2011-12-19 10:08:15