java - Mavn執行測試時<scope>test</scope>導致錯誤
PHPz
PHPz 2017-05-17 10:08:51
0
1
932

學習maven test時,執行mvn test時,會找不到org.junit
在pom.xml中已經引入

    <dependencies>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

報錯資訊如下

檔案目錄如下

#hello目錄下存在如下檔案

其中GreeterTest為測試

執行mvn compile 或mvn package也會報錯

#當把pom.xml中junit依賴的scope去掉時,編譯和測試都能成功。

    <dependencies>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

造成這個的原因是什麼? maven在執行compile時同時編譯*Test的檔案嗎,那麼為什麼mvn test也不能成功?
mvn test不是會自動執行*Test的檔案嗎?而且scope test確定了測試時會引入junit

#
PHPz
PHPz

学习是最好的投资!

全部回覆(1)
phpcn_u1582

這個問題其實你因為你不熟悉maven檔案結構所致.測試類別一般是放在src/test/java,而不是放在src/main/java下.maven在編譯的時候,src/main/java下是不引用test的jar,而編譯src/test/java下的測試這會引用test的jar

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!