Theo
Theo

Reputation: 3139

Roboelectric testing

I have never used any kind of JUnit before,so from what I search there is a good tool called roboelectric.

I try to run a simple test as shown in their website but the assertThat() and shadowOf() are not recognised.

Here is the test

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class MainActivityTest {

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

    Intent expectedIntent = new Intent(activity, LoginActivity.class);
    //This line doesn't work.         
    assertThat(shadowOf(activity).getNextStartedActivity()).
    isEqualTo(expectedIntent);

  }
 }

And this how I set Roboelectric in gradle.

apply plugin: 'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion "24.0.0"

defaultConfig {
    applicationId "testing.theo.robotutorial"
    minSdkVersion 14
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),   
'proguard-rules.pro'
    }
   }
 }

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:24.0.0'
  testCompile "org.robolectric:robolectric:3.0"


 }

Any ideas?

Thanks.

Upvotes: 0

Views: 60

Answers (1)

gambler2000
gambler2000

Reputation: 21

Replace with the following assertion will work.

assertTrue(shadowOf(activity).getNextStartedActivity().filterEquals(expectedIntent));

You can refer to answer by Hoisie in Roboelectric github issue #2627 for details.

Upvotes: 1

Related Questions