Home » Android » android – importing correct AssertThat method for Robolectric Test

android – importing correct AssertThat method for Robolectric Test

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:

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'

Answer:

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