2012-12-20 64 views
3

我使用的Maven與JUnit測試。我使用標準的Maven項目結構,我可以在ecplise中運行「作爲Junit測試運行」,並且它們都成功了,但是如果我想運行maven測試/安裝,那麼測試不運行,因爲「無法初始化class main.sushi .persistence.Persistor」 我們的項目樹:Maven的JUnit測試 「的NoClassDefFoundError」

├── pom.xml 
├── src 
│   ├── main 
│   │   ├── java 
│   │   │   └── main 
│   │   │    └── sushi 
│   │   │     └── persistence 
│   │   │      ├── DatabaseEnvironments.java 
│   │   │      ├── META-INF 
│   │   │      │   ├── persistence.xml 
│   │   │      │   └── persistence_template.xml 
│   │   │      └── Persistor.java 
│   │   └── resources 
│   └── test 
│    ├── java 
│    │   ├── META-INF 
│    │   │   ├── persistence.xml 
│    │   │   └── persistence_template.xml 
│    │   └── test 
│    │    └── sushi 
│    │     └── persistence 
│    │      ├── ProcessPersistorTest.java 
│    │      └── TestPersistor.java 
│    └── resources 
└── target 
    ├── classes 
    │   ├── META-INF 
    │   │   ├── MANIFEST.MF 
    │   │   └── maven 
    │   │    └── sushi 
    │   │     └── SushiPersistence 
    │   │      ├── pom.properties 
    │   │      └── pom.xml 
    │   └── main 
    │    └── sushi 
    │     └── persistence 
    │      ├── DatabaseEnvironments.class 
    │      ├── META-INF 
    │      │   ├── persistence.xml 
    │      │   └── persistence_template.xml 
    │      └── Persistor.class 
    ├── generated-sources 
    │   └── annotations 
    ├── surefire 
    ├── surefire-reports 
    │   ├── TEST-test.sushi.persistence.ProcessPersistorTest.xml 
    │   ├── TEST-test.sushi.persistence.TestPersistor.xml 
    │   ├── test.sushi.persistence.ProcessPersistorTest.txt 
    │   └── test.sushi.persistence.TestPersistor.txt 
    └── test-classes 
     ├── META-INF 
     │   ├── persistence.xml 
     │   └── persistence_template.xml 
     └── test 
      └── sushi 
       └── persistence 
        ├── ProcessPersistorTest.class 
        └── TestPersistor.class 

POM:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>sushi</groupId> 
    <artifactId>SushiPersistence</artifactId> 
    <version>SNAPSHOT</version> 
    <repositories> 
     <repository> 
      <id>EclipseLink</id> 
      <url>http://download.eclipse.org/rt/eclipselink/maven.repo</url> 
     </repository> 
    </repositories> 
    <build> 

     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.7</source> 
        <target>1.7</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>de.hpi-web.sushicommon</groupId> 
      <artifactId>SushiCommon</artifactId> 
      <version>SNAPSHOT</version> 
     </dependency> 
     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>4.11</version> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.persistence</groupId> 
      <artifactId>eclipselink</artifactId> 
      <version>2.3.0</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.eclipse.persistence</groupId> 
      <artifactId>org.eclipse.persistence.jpa</artifactId> 
      <version>2.0.0</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.22</version> 
     </dependency> 
    </dependencies> 


</project> 
+1

是你能解決這個問題?我有完全相同的問題。所有junit測試都在Eclipse中運行,但在使用Maven運行時出現錯誤(在命令行中) – Velth

+0

我認爲主要問題是我們在eclipse中的其他項目中引用了類。 Eclipse有點允許這個。 Maven在這裏更加嚴格。 – ThommyH

回答

1

的src/main /壽司/持久/ META-INF的資源有下SRC將位於/ main/resources和適當的測試文件夾必須是在src/test/resources而不是src/main/javasrc/test/java

+0

謝謝你的建議,但它沒有固定的classdefnotfounderror :(它有點怪,與ecplise我可以運行 – ThommyH

+0

你可以添加你有寫noClassFound代替或**「無法初始化類主要的完整的錯誤消息的JUnit測試。 sushi.persistence.Persisto「**等?除此之外,你應該遵循Maven的約定,不要嘗試去的其他方式。 – khmarbaise

0

所有你所依賴的刪除範圍的標籤。目前u必須設置編譯所以行家做什麼你問它做..意味着它不包括在測試階段,你的依賴。

+0

試了一下(設置爲‘測試’,並試圖‘規定’),但仍相同的。 實際上可以,他沒有找到類是項目本身,所以它不是一個依賴問題:( – ThommyH

+0

ü不需要範圍標記在所有....刪除它..然後嘗試... – Chetan

+0

範圍「compile」是默認的作用域。根據文檔,刪除作用域不會執行任何操作:http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope –