【问题标题】:Android JUnit Testing, cannot resolve is(boolean) in assertThat?Android JUnit 测试,无法解析 assertThat 中的 is(boolean)?
【发布时间】:2016-07-14 23:37:59
【问题描述】:

在 build.gradle 中

dependencies {    
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
}

代码

import com.myapp.LVActivity;
import org.junit.Test;
import static org.junit.Assert.*;

public class DeviceUnitTest {
    @Test
    public void check_that_is_correct_device_name_isTrue() {
        assertThat(LVActivity.isCorrectDevice("MySpecialDevice"), is(true));
    }
}

在 LVActivity 中:

private final static String correctName = "MySpecialDevice";
public static boolean isCorrectDevice(String deviceName) {
    return deviceName.equals(correctName);
}

错误在这里:

is(true)

错误:

Cannot resolve method is(boolean)

我正在尝试做一个简单的本地单元测试。

我正在关注这个教程http://developer.android.com/training/testing/unit-testing/local-unit-tests.html

【问题讨论】:

  • 您需要静态导入 Matchers.is 或完全限定调用以说明您要使用 Matchers 类中的 is 方法。
  • 这个问题节省了我的时间..谢谢

标签: android unit-testing junit boolean local


【解决方案1】:

你不想说:

import static org.junit.Assert.*;

assertTrue(LVActivity.isCorrectDevice("MySpecialDevice"));

这是检查布尔值的更自然的表达式。

但如果您需要将is 匹配器与assertThat 一起使用,则需要此导入:

import static org.hamcrest.CoreMatchers.*;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-22
    相关资源
    最近更新 更多