2014-06-15 69 views
2

我有一個測試,在Eclipse中完美的工作,但在Gradle失敗。我不知道什麼是錯的。我在Eclipse中使用Java 8。測試在Eclipse中傳遞,但在Gradle中失敗。關於assertThat

提示:

E:\Files\Source\Workspace-Eclipse2\project\src\test\java\com\project\core\domain\TeamUnitTest.java:31: error: no sui 
table method found for assertThat(List<User>,Matcher<Collection<Object>>) 
       assertThat(team.getUsers(), empty()); 
       ^
    method Assert.<T#1>assertThat(T#1,Matcher<? super T#1>) is not applicable 
     (actual argument Matcher<Collection<Object>> cannot be converted to Matcher<? super List<User>> by method invocati 
on conversion) 
    method Assert.<T#2>assertThat(String,T#2,Matcher<? super T#2>) is not applicable 
     (cannot instantiate from arguments because actual and formal argument lists differ in length) 
    where T#1,T#2 are type-variables: 
    T#1 extends Object declared in method <T#1>assertThat(T#1,Matcher<? super T#1>) 
    T#2 extends Object declared in method <T#2>assertThat(String,T#2,Matcher<? super T#2>) 
E:\Files\Source\Workspace-Eclipse2\project\src\test\java\com\project\core\domain\TeamUnitTest.java:51: error: no sui 
table method found for assertThat(List<User>,Matcher<Collection<Object>>) 
       assertThat(team.getUsers(), empty()); 
       ^
    method Assert.<T#1>assertThat(T#1,Matcher<? super T#1>) is not applicable 
     (actual argument Matcher<Collection<Object>> cannot be converted to Matcher<? super List<User>> by method invocati 
on conversion) 
    method Assert.<T#2>assertThat(String,T#2,Matcher<? super T#2>) is not applicable 
     (cannot instantiate from arguments because actual and formal argument lists differ in length) 
    where T#1,T#2 are type-variables: 
    T#1 extends Object declared in method <T#1>assertThat(T#1,Matcher<? super T#1>) 
    T#2 extends Object declared in method <T#2>assertThat(String,T#2,Matcher<? super T#2>) 
2 errors 
:compileTestJava FAILED 

測試代碼:

assertThat(team.getUsers(), empty()); 
+2

很可能是Gradle構建正在使用不喜歡此代碼的早期Java版本進行編譯。通過'gradle -v'檢查顯示哪個JVM版本。 –

+0

@PeterNiederwieser我會試試這個。 – Chad

+2

向Hamcrest 1.3添加顯式依賴並使用JUnit 4.11。 –

回答

3

這似乎是你的gradle這個版本是不一樣的JUnit拉或hamcrest Eclipse是,而是一老一。你應該設置明確的依賴關係。

如果你把這個類轉換src/test/java/SOTest.java

import org.junit.Test; 
import static org.junit.Assert.assertEquals; 
import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.Matchers.*; 

import java.util.Collections; 
import java.util.List; 

public class SOTest { 
    @Test 
    public void doTest() throws Exception { 
    // Regular JUnit assert 
    assertEquals(true, true); 

    // Hamcrest assertThat 
    List<Object> teamUsers = Collections.emptyList(); 
    assertThat(teamUsers, empty()); 
    } 
} 

然後創建一個build.gradle文件,這在:

apply plugin: 'java' 
sourceCompatibility = 1.6 

repositories { 
    mavenCentral() 
} 

dependencies { 
    testCompile group: 'junit', name: 'junit', version: '4.+' 
    testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3' 
    testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3' 
} 

而建,中assertThat(Collection,empty())測試將運行並通過。

這裏的區別在於我們明確地依賴於新版本的junit,以及具有這些匹配的hamcrest 1.3版本。

相關問題