简体   繁体   中英

importing correct AssertThat method for Robolectric Test

I am attempting to run the test from Robolectric.org's Writing Your First Test page. The test in question looks like this:

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

I get this compile error: Cannot resolve method 'assertThat(android.content.Intent) .

The two possibilities I see for importing this method are org.hamcrest.MatcherAssert.assertThat and org.junit.Assert.assertThat , neither of which have a single-argument assertThat method as is being used in this Robolectric test.

My app's build.gradle has these dependencies:

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'
}

What framework/library is this test using?

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'

follow the following and the issue should go away. Put the first line in you 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); 
     }
}  

this link should provide more details also Android Studio and Robolectric

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM