无法解析符号 assertThat

Cannot resolve symbol assertThat

我的 build.gradle 文件中有以下依赖项

 testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.9.5'

而我的测试class EmailValidatorTest有如下代码

  @Test
public void emailValidator_simpleEmail_returnsTrue(){

   assertThat(EmailValidator.isValidEmail("name@ex.com"),is(true))
}

但我得到的错误是 Cannot resolve symbol assertThat。我只得到 assert 对象。我目前正在处理来自 Android Developers 的样本,即:https://github.com/googlesamples/android-testing/tree/master/unit/BasicSample.

确保您导入了 assertThat.

public static <T> void assertThat(T actual,
                                  org.hamcrest.Matcher<T> matcher)

import static org.hamcrest.MatcherAssert.assertThat;

然后清理-重建-运行 .

我遇到了同样的问题。以下是对我有用的内容:

在 app/build.gradle:

testImplementation 'com.google.truth:truth:0.43'

在 EmailValidatorTest class:

import com.google.common.truth.Truth;

和内部 emailValidator_simpleEmail_returnsTrue() 方法:

Truth.assertThat(EmailValidator.isValidEmail("name@ex.com"),is(true))

看,你不直接导入'assertThat',和教程里说的相反

Here an example.