为 Robolectric 测试导入正确的 AssertThat 方法

importing correct AssertThat method for Robolectric Test

我正在尝试 运行 来自 Robolectric.org 的 Writing Your First Test 页面的测试。有问题的测试如下所示:

  @Test
  public void clickingLogin_shouldStartLoginActivity() {
    WelcomeActivity activity = Robolectric.setupActivity(WelcomeActivity.class);
    activity.findViewById(R.id.login).performClick();

    Intent expectedIntent = new Intent(activity, WelcomeActivity.class);
    assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent);
  }

我得到这个编译错误:Cannot resolve method 'assertThat(android.content.Intent)

我看到导入此方法的两种可能性是 org.hamcrest.MatcherAssert.assertThat and org.junit.Assert.assertThat,这两种可能性都没有此 Robolectric 测试中使用的单参数 assertThat 方法。

我的应用 build.gradle 具有以下依赖项:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:design:23.1.0'

    testCompile "org.robolectric:robolectric:3.0"
    testCompile 'junit:junit:4.12'
}

这个测试使用什么framework/library?

既不是junit也不是hamcrest断言API。我认为是 Android AssertJ 或只是 AssertJ:

testCompile 'org.assertj:assertj-core:1.7.1'

按照以下步骤操作,问题应该会消失。将第一行放在 gradle build file

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

import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class SomethingTest {
     @Test
     public void testSomething() {
           assertThat(true, 1>1); 
     }
}  

这个 link 也应该提供更多细节 Android Studio and Robolectric