很抱歉,这在这里不太明确,但是我不允许发布我的整个实际代码。我只能说是作为./gradlew connectedAndroidTest的一部分运行此测试时遇到问题
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MobileAppSanityTest extends AbstractEspressoTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ClearPreferencesActivityTestRule<>(MainActivity.class, getFiles());
@Override
protected Context getContext() {
return mActivityRule.getActivity();
}
@BeforeClass
public static void beforeAll() {
RoboGuice.Util.reset();
}
@Test
public void test_SingleUserFlow() {
navigateSplashScreen();
logIn();
doSomethingElse();
}
}
这里发生的是,当我自己运行此测试类时-它运行良好,但是当我将其作为“ connectedAndroidTest”的一部分运行时,活动将在“ navigateSplashScreen”之后立即停止,并且无法执行登录。 / p>
我得到的错误是:
java.lang.RuntimeException: No activities found. Did you t to launch the activity by calling getActivity() or startActivitySync or similar?
总体来说,我对Espresso和Android还是陌生的,所以很难为我着迷。如果您需要更多信息,请告诉我。如果是这样,我会尽力提供。
答案 0 :(得分:0)
jUnit TestCase
的外观有所不同;认为在那里只能使用Espresso
。
@RunWith(AndroidJUnit4.class)
public class MainActivityTest extends TestCase {
/** Log Tag */
private static final String LOG_TAG = MainActivityTest.class.getSimpleName();
/** the Activity of the Target application */
private MainActivity mActivity;
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>(MainActivity.class) {
};
@Override
public void setUp() throws Exception {
super.setUp();
}
/* obtaining the context from the ActivityTestRule */
@Before
public void setUpTest() {
this.mActivity = this.mActivityRule.getActivity();
}
/* add Espresso code eg. here */
@Test
@UiThreadTest
public void navigateSplashScreen() {
}
@Override
public void tearDown() throws Exception {
super.tearDown();
}
}