Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 636
Michiyo Avatar asked Oct 28 '15 15:10

Michiyo


People also ask

Why assertThat is deprecated?

assertThat method is deprecated. Its sole purpose is to forward the call to the MatcherAssert. assertThat method defined in Hamcrest 1.3. Therefore, it is recommended to directly use the equivalent assertion defined in the third party Hamcrest library.

How robolectric works?

Robolectric works by creating a runtime environment that includes the real Android framework code. This means when your tests or code under test calls into the Android framework you get a more realistic experience as for the most part the same code is executed as would be on a real device.

What is the significance of Robolectric?

Robolectric provides a JVM compile version of the android. jar file. Robolectric handles views, resource loading, and many other things that are implemented in the Android native. This enables you to run your Android tests in your development environment, without requiring any other setup to run the test.


2 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'
like image 122
Eugen Martynov Avatar answered Sep 28 '22 19:09

Eugen Martynov


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

like image 32
display name Avatar answered Sep 28 '22 18:09

display name