2017-10-10 82 views
0

我想要包含/排除JUnit分類測試。我在通用模塊中定義了標記界面StressTest。我在moduleA中引用StressTest。我有以下根pom.xml具有多模塊項目的JUnit類別 - 無法加載類別

<plugin> 
<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-surefire-plugin</artifactId> 
<version>2.20</version> 
<configuration> 
    <excludedGroups>com.mycompany.project.common.utils.StressTest</excludedGroups> 
</configuration> 
</plugin> 

Maven的萬無一失,插件在運行MVN測試,我得到同時建立另一個模塊以下。

Unable to load category: com.mycompany.project.common.utils.StressTest 

我應該在哪裏寫我的StressTest接口?

回答

0

爲了確保'找到'您的@Category類,它必須位於Maven項目的依賴關係樹生成的類路徑中。

此異常......

無法加載類:com.mycompany.project.common.utils.StressTest

...強烈暗示,無論神器包含com.mycompany.project.common.utils.StressTest您的moduleA聲明的依賴關係。

因此,您需要在您的moduleA上添加對任何工件包含com.mycompany.project.common.utils.StressTest的依賴關係。如果這種依賴性的唯一目的是提供@Category類,那麼使這種依賴性測試的作用範圍是有意義的,例如,

<dependency> 
    <groupId>com.mycompany</groupId> 
    <artifactId>common.utils</artifactId> 
    <version>...</version> 
    <scope>test</scope> 
</dependency> 

此外,如果com.mycompany.project.common.utils.StressTest測試樹,而不是主要樹在您共同utils的模塊,那麼你將需要創建一個包含普通utils的模塊的測試類的JAR。您可以通過添加以下maven-jar-plugin聲明共同utils的pom.xml中做到這一點:

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-jar-plugin</artifactId> 
    <version>3.0.2</version> 
    <executions> 
     <execution> 
     <goals> 
      <goal>test-jar</goal> 
     </goals> 
     </execution> 
    </executions> 
    </plugin> 

你會那麼依賴於此的依賴性使用<type>test-jar</type>moduleA,例如:

<dependency> 
    <groupId>com.mycompany</groupId> 
    <artifactId>common.utils</artifactId> 
    <version>...</version> 
    <type>test-jar</type> 
    <scope>test</scope> 
</dependency> 
+0

如果我在測試/ java下定義StressTest,那麼它不起作用,但是如果我在main/java中定義它,那麼它就可以工作。 – bluetech

+0

@bluetech我已經更新了答案,描述瞭如何導出並依賴於來自您的通用utils模塊的'test-jar'。 – glytching