簡體   English   中英

如果我在 Maven 測試中根本沒有使用 hamcrest,為什么我會得到 NoClassDefFound org/hamcrest/SelfDescribing?

[英]Why am I getting NoClassDefFound org/hamcrest/SelfDescribing if I'm not using hamcrest at all in my Maven test?

我已經有一段時間了。 StackOverflow 中的相關問題是關於項目的classpath 中沒有hamcrest-core JAR,解決方法都與它的添加有關。 我想做的是相反的:從類路徑中刪除這個依賴項。

考慮一個帶有單個測試用例的 Maven 項目:

import static org.junit.Assert.assertTrue;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest {

    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue() {
        assertTrue(true);
    }
}

這里的測試shouldAnswerWithTrue調用方法assertTrue在類Assert從依賴關系junit版本4.11(POM中聲明)。 當我構建相應的調用圖時,這個hamcrest-core似乎沒有使用依賴項hamcrest-core hamcrest-core是由直接依賴junit引起的傳遞依賴。 因此,我繼續將它從我的項目的 POM 中排除,如下所示:

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

但是,當我執行mvn package ,它會觸發以下錯誤:

java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing

我不明白為什么 Java 在我的測試及其任何方法調用中根本沒有使用的依賴SelfDescribing中抱怨接口SelfDescribing 我已經檢查過hamcrest-core中的任何類都沒有從 JUnit 中的Assert類加載。

那么,為什么我不能排除hamcrest-core 為什么需要這個接口? 它在哪里叫?

因為 JUnit 4.11 實際上在編譯時依賴於它: 它在其異常層次結構中使用它 當加載AssumptionViolatedException類時,它會觸發加載SelfDescribing

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM