我以前从来没有使用过任何类型的JUnit,所以从我搜索到的结果中,有一个很好的工具叫做roboelectric。
我试着运行一个简单的测试,如他们的网站所示,但是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"
}有什么想法吗?
谢谢。
发布于 2017-02-14 15:30:14
替换为以下断言将起作用。
assertTrue(shadowOf(activity).getNextStartedActivity().filterEquals(expectedIntent));您可以参考Roboelectric github issue #2627中的answer by Hoisie了解详细信息。
https://stackoverflow.com/questions/38118068
复制相似问题