2016-06-30 7 views
0

以前はJUnitを一度も使用していませんでしたので、私が検索したものからはroboelectricという優れたツールがあります。Roboelectric testing

ウェブサイトに示されているように簡単なテストを実行しようとしましたが、assertThat()とshadowOf()は認識されません。

はここでテスト

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

    } 
} 

そして、この方法を私はGradleの中Roboelectricを設定しています。

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" 


} 

ありがとうございました。

答えて

1

次のアサーションで置き換えて動作します。

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

あなたは詳細についてはRoboelectric github issue #2627Hoisieで答えるために参照することができます。