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

时间:2015-10-28 15:34:31

标签: android unit-testing robolectric

我正在尝试从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.assertThatorg.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'
}

此测试使用什么框架/库?

2 个答案:

答案 0 :(得分:17)

It is neither junit or hamcrest assertion API. I think it is Android AssertJ or just AssertJ:

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

答案 1 :(得分:2)

按照以下说明,问题应该消失。将第一行放在gradle构建文件中

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

此链接还应提供更多详细信息 Android Studio and Robolectric