2016-01-29 2 views
1

Я кодирования тесты с использованием JUnit 4.11 и Hamcrest 1.1 со следующей конфигурацией:JUnit бросает java.lang.NoClassDefFoundError: орг/Hamcrest/MatcherAssert

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

<dependency> 
    <groupId>org.hamcrest</groupId> 
    <artifactId>hamcrest-all</artifactId> 
    <version>1.1</version> 
    <scope>test</scope> 
</dependency> 

И когда я запускаю их, появляется следующее исключение:

java.lang.NoClassDefFoundError: org/hamcrest/MatcherAssert 

В строке, содержащей:

assertThat(obj, hasProperty('id', 1L)) 

ответ

1

Вы должны использовать hamcrest-library вместо hamcrest-all. hamcrest-all не предназначен для использования с менеджером зависимостей, потому что это uber-jar, который содержит hamcrest-core и hamcrest-library. Следующий фрагмент должен работать.

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

<dependency> 
    <groupId>org.hamcrest</groupId> 
    <artifactId>hamcrest-library</artifactId> 
    <version>1.3</version> 
    <scope>test</scope> 
</dependency> 
+0

Thnx попробует его на работе;) – Alfergon

1

Junit имеет свою зависимость от org.hamcrest:hamcrest-core:1.3:compile.

проблема была решена путем изменения зависимостей в:

<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> 

<dependency> 
    <groupId>org.hamcrest</groupId> 
    <artifactId>hamcrest-all</artifactId> 
    <version>1.3</version> 
    <scope>test</scope> 
</dependency> 
Смежные вопросы