2016-07-15 267 views
-3

我有一些項目的單元測試,我在intellij想法中執行。如果我使用java 7,測試工作正常,但是當我將java從7更改爲8時,所有單元測試都會出現此錯誤堆棧跟蹤。JUNIT4在java下無法工作8

java.lang.IllegalArgumentException 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) 
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

什麼是這個問題的根本原因?

更新 - 添加測試類和標題的導入。

import junit.framework.TestCase; 
import mockit.Expectations; 
import mockit.NonStrictExpectations; 
import mockit.Mocked; 

public class PrepareModule extends TestCase { 
+1

看起來問題不在JUNIT它是在intellij – Jens

+0

請添加一些代碼。 – cssGEEK

+0

您是否使用最新版本的IntelliJ? – Tom

回答

0

你說你正在使用的JUnit 4,但您的代碼段包括:

import junit.framework.TestCase; 

public class PrepareModule extends TestCase { 
} 

這是通過測試的JUnit的3風格

試試這樣說:

import org.junit.Test; 

public class PrepareModule { 
    @Test 
    public void someTest() { 
    } 
} 
+0

仍然得到相同的錯誤。 –

+1

你可以檢查JUnit 3的依賴關係並刪除它們嗎?這個答案的方法似乎是正確的。 – vikingsteve

0

OK,wit^h新鮮的眼睛看着你的堆棧跟蹤:

java.lang.IllegalArgumentException 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 

假設這是整個堆棧,我看包僅從:

  • java.lang
  • sun.reflect
  • com.intellij

這告訴我JUnit無關與問題;也可能是問題出在頂部的類(或底部爲準)堆棧:com.intellij.junit4.JUnit4IdeaTestRunner

,如果你可以嘗試不同的JUnit運行,如from command line

java org.junit.runner.JUnitCore <test class name> 
0

什麼版本JMockIt你使用?一些老版本的JMockIt與Java 8不兼容。

我和你幾乎有同樣的問題(一切都一樣,除了我的測試沒有擴展TestCase)。我通過升級JMockIt 1.0 - > 1.14解決了我的問題。

0

我覺得這個問題是由IDEA引入的。

+0

這不提供問題的答案。一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你將可以在任何帖子上[評論](http://stackoverflow.com/help/privileges/comment)。另外檢查這[我可以做什麼,而不是](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an-i-do-instead )。 – thewaywewere