Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Testing in Android Unit Testing - Now Featuring Robolectric! Shadows and Intents

Ruslan Shakolo
Ruslan Shakolo
4,528 Points

Getting the wrong intent from getNextStartedActivity()

i don't know why, but i am getting MainActivity intent from getNextStartedActivity() with the same code. here is my MainActivityTest file:

import android.content.Intent;

import android.graphics.Color;

import android.graphics.drawable.ColorDrawable;

import android.os.Build;

import android.view.inputmethod.EditorInfo;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.robolectric.Robolectric;

import org.robolectric.RobolectricGradleTestRunner;

import org.robolectric.Shadows;

import org.robolectric.annotation.Config;

import org.robolectric.shadows.ShadowActivity;

import static junit.framework.TestCase.assertTrue;

import static org.junit.Assert.assertEquals;

@RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP) public class MainActivityTest { MainActivity activity;

@Before
public void setUp()
{
   activity = Robolectric.setupActivity(MainActivity.class);
}

@Test
public void editTextApdatesTextView() throws Exception {
   // arrange
  String givenString = "test123";
  activity.editText.setText(givenString);

   // Act
   activity.editText.onEditorAction(EditorInfo.IME_ACTION_DONE);


 // Assert
   String actualString = activity.textView.getText().toString();
  assertEquals(givenString,actualString);
}
@Test
public void spinnerUpdatesBackgroundColor() throws  Exception {
    //Arrange
    int index = 2;
    int givenColor = Color.GREEN;
    //Act
    activity.colorSpinner.setSelection(index);
    //Assert
    int actualColor = ((ColorDrawable) activity.linearLayout.getBackground()).getColor();
    assertEquals(givenColor,actualColor);
}
@Test
public void buttonLaunchesOtherActivity() throws  Exception {
    //Arrange
    Class clazz = OtherActivity.class;
    Intent expectedIntent = new Intent(activity,clazz);
    //Act
    activity.launchActivityButton.callOnClick();
    //Assert
    ShadowActivity shadowActivity = Shadows.shadowOf(activity);
    Intent actualIntent = shadowActivity.getNextStartedActivity();
    assertEquals(expectedIntent,actualIntent);
}

} maybe i've added the wrong import? i don't know what to do.

Ben Deitch Sir can you please help..... When I'm trying to run my this code

        ShadowActivity shadowActivity= Shadows.shadowOf(activity);
        Intent actualIntent = shadowActivity.getNextStartedActivity();
        assertTrue(expectedIntent.filterEquals(actualIntent));

I'm getting this error

Error:(73, 47) error: cannot access ShortcutManage class file for android.content.pm.ShortcutManager not found

My imports are

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.inputmethod.EditorInfo;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowActivity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Ben Deitch
Ben Deitch
Treehouse Teacher

Hey Varun!

I've not seen that error before, but normally I'd look for somewhere in my code that uses the ShortcutManage class. Unfortunately, it doesn't look like we have that here, so I'm not really sure what's causing the error :/

Which line is throwing the error?

Hello Ben Deitch Sir, I just switch to API 27. I mean I changed my build.config file targetedSDk and buildTools to 27 and now my tests are passing but I'm getting these warnings "here" and my new build.config file looks something like this. And thanks for a responding to my comment :)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    buildToolsVersion '27.0.0'

    defaultConfig {
        applicationId "com.teamtreehouse.testingbase"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:27.0.0'

    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.10.19"
    testCompile "org.robolectric:robolectric:3.5.1"

    androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'
    androidTestCompile 'com.android.support.test:runner:1.0.1'
}

1 Answer

Chris Gaona
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Gaona
Full Stack JavaScript Techdegree Graduate 31,350 Points

I ran into the same ShortcutManager error. I updated my build.gradle sdk versions to 27 and the tests started to pass just fine. Maybe it's a backwards compatibility issue of Roboelectric or something.