java - 测试在 Eclipse 中通过但在 Gradle 中失败。与 assertThat 有关

标签 java junit gradle

我有一个在 Eclipse 中完美运行但在 Gradle 中失败的测试。我不确定哪里出了问题。我在 Eclipse 中使用 Java 8。

提示:

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());

最佳答案

看起来您的 gradle 构建并没有引入与 Eclipse 相同的 junit 或 hamcrest,而是一个较旧的。您应该设置显式依赖项。

如果你把这个类放到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 版本。

关于java - 测试在 Eclipse 中通过但在 Gradle 中失败。与 assertThat 有关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24226029/

相关文章:

java - 单击按钮时暂停-恢复音频

java - JUnit 运行程序从 java.io.Writer 获取 NullPointerException

java - java.lang.Object getTimeout() 的返回类型与 org.gradle.api.internal.AbstractTask 中的 org.gradle.api.provider.Property 不兼容

plugins - Gradle:重新初始化/重新加载settings.gradle

java - 如何使用 Gradle 添加默认 JVM 参数

java - 如何通过同一个套接字发送不同类型的数据

java - 为什么数组被初始化为默认值而不是 java 中的 arraylist?

java - 为什么有时这不起作用?

java - 无法使用 Maven 访问资源目录中的文件(使用 Eclipse IDE)

java - Spring Boot - 模拟对外部 API 的 POST REST 请求