2012-02-28 141 views
2

我正在用maven,scala和junit編寫一個簡單的項目。Maven項目找不到junit

我發現的一個問題是我的測試找不到org.junit.framework.Test。測試文件:

import org.junit.framework.Test 

class AppTest { 
    @Test 
    def testOK() = assertTrue(true) 

    @Test 
    def testKO() = assertTrue(false) 
} 

返回錯誤:

[WARNING]..... error: object junit is not a member of package org 
[WARNING] import org.junit.framework.Test 
[WARNING]   ^

我確實有增加的JUnit作爲一個依賴,它清楚地坐在我的倉庫裏。

 <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.8.1</version> 
      <scope>test</scope> 
     </dependency> 

有人可以告訴我是什麼原因造成的嗎?

非常感謝

+1

測試文件是否在測試文件夾中?如果不是的話,測試的範圍將會搞砸一切。嘗試刪除範圍標籤,看看是否有幫助。 POM中的其餘依賴關係在這裏看起來很好。其次,你的AppTest類是什麼包? – 2012-02-28 18:39:22

+0

感謝您的回覆。是的,測試代碼位於測試文件夾中。該文件位於包com.mycompany.myservice中。 – Kevin 2012-02-28 19:16:15

+0

你的.m2文件夾中是否有junit .jars文件?我認爲這是〜/ .m2 /默認存儲庫。 – 2012-02-28 21:04:00

回答

1

包中的JUnit 4.8.1的Testjunit.framework.Test而不是org.junit.framework.Test

+0

關閉,但沒有雪茄。它實際上是org.junit.Test。請參閱http://junit.sourceforge.net/javadoc/org/junit/Test.html。它不回答爲什麼org.junit沒有找到。 – 2012-02-28 20:07:41

+0

我不確定,但是您指出的文檔是JUnit 3.8,不是4.8。如果您查看JUnit jar(http://search.maven.org/#artifactdetails%7Cjunit%7Cjunit%7C4.8.1%7Cjar),您會看到導入是junit.framework.Test。 – 2012-02-28 21:36:32

+0

你說得對,請嘗試http://kentbeck.github.com/junit/javadoc/latest/org/junit/Test.html。 OP使用的註解@Test是org.junit.Test。 – 2012-02-29 06:02:45

2

由於@nico_ekito說,您的導入不正確,應該是org.junit.Test

但是這並不能說明你的問題:

[WARNING]..... error: object junit is not a member of package org 
[WARNING] import org.junit.framework.Test 
[WARNING]   ^

所以它不能找到org.junit。這意味着在編譯測試時,org.junit不在類路徑中。因此,無論你的依賴樹是搞砸了,還是實際的jar。

嘗試

mvn dependency:list 

這將導致類似:

[INFO] The following files have been resolved: 
[INFO] junit:junit:jar:4.8.1:test 

確保你有被解決沒有其他的JUnit庫。如果一切正常,請檢查您的junit罐的內容。它應該包含org.junit.Test類。如果沒有,你有一個損壞的回購,或者你沒有找到正確的地方。要知道最簡單的方法是使用:

mvn dependency:copy-dependencies 

與依賴的副本創建目標/依賴。你可以看看那裏的罐子。