java是否可以在@BeforeClass注释方法中运行Android Espresso单元测试?
我在插入指令的Android单元测试中使用JUnit4注释时遇到了一个问题(我使用的是EspressoGUI测试库)。只要我添加一个带有@BeforeClass
注释的测试,安卓studio 1.5.1就不会运行任何测试,而是只打印“空测试套件”。我没有使用测试套件。我搜索了这个网站和网络,但找不到解决方案。我认为在@BeforeClass
方法中调用的代码实际上失败(TDD)可能是一个问题,但是当在正常测试用例中工作的代码被放入@BeforeClass
注释的方法中时,甚至会发生这个错误
多谢各位
更新:在检查logcat输出后,正如一位评论员所建议的,问题似乎在于没有启动任何活动:没有找到任何活动。您是否忘记通过调用getActivity()
或startActivitySync
或类似命令来启动活动
我该怎么做?我不能使用ActivityTestRule
字段,因为@BeforeClass
注释的方法是静态的
也许我只是以错误的方式使用了@BeforeClass
注释。我的印象是,您可以使用此注释在测试类中的所有其他测试之前执行测试。我基本上是在这里寻找TestNG注释的替代品。也许我最好在测试类上使用@FixMethodOrder(MethodSorters.NAME_ASCENDING)
注释,并将第一个测试用例重命名为aaa_my_testcase
有人能对此发表评论吗?谢谢
重新措辞问题的标题
import 安卓.support.test.rule.ActivityTestRule;
import 安卓.support.test.runner.AndroidJUnit4;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import static 安卓.support.test.espresso.Espresso.onView;
import static 安卓.support.test.espresso.Espresso.pressBack;
import static 安卓.support.test.espresso.action.ViewActions.click;
import static 安卓.support.test.espresso.assertion.ViewAssertions.matches;
import static 安卓.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static 安卓.support.test.espresso.matcher.ViewMatchers.isEnabled;
import static 安卓.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static 安卓.support.test.espresso.matcher.ViewMatchers.withId;
import static 安卓.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.not;
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> menuActivityTestRule = new ActivityTestRule<>(MainActivity.class);
private static void checkSignBrowserIsDisplayed() {
onView(withText(R.string.sign_browser)).check(matches(isDisplayed()));
}
@BeforeClass
public static void checkSignBrowserIsDisplayedOnAppStartup() {
checkSignBrowserIsDisplayed();
}
构建。应用程序:
apply plugin: 'com.安卓.application'
安卓 {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "foo"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "安卓.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-安卓.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
}
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'junit:junit:4.12'
安卓TestCompile 'junit:junit:4.12'
安卓TestCompile 'com.安卓.support:support-annotations:23.1.1'
安卓TestCompile 'com.安卓.support.test:runner:0.4.1'
安卓TestCompile 'com.安卓.support.test:rules:0.4.1'
安卓TestCompile 'org.hamcrest:hamcrest-library:1.3'
安卓TestCompile 'com.安卓.support.test.espresso:espresso-core:2.2.1'
// required if you want to use Mockito for Android instrumentation tests - not needed now.
// 安卓TestCompile 'org.mockito:mockito-core:1.+'
// 安卓TestCompile "com.google.dexmaker:dexmaker:1.2"
// 安卓TestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'org.apache.commons:commons-lang3:3.4'
compile 'com.安卓.support:appcompat-v7:23.1.1'
compile 'com.安卓.support:design:23.1.1'
}
测试输出:
Running tests
Test running startedFinish
Empty test suite.
Logcat输出:
01-25 10:22:42.746 22098-22118/foo I/TestRunner: run started: 5 tests
01-25 10:22:42.764 22098-22118/foo D/InputManagerEventInjectionStrategy: Creating injection strategy with input manager.
01-25 10:22:42.890 22098-22118/foo I/TestRunner: failed: foo.MainActivityTest
01-25 10:22:42.890 22098-22118/foo I/TestRunner: ----- begin exception -----
01-25 10:22:42.891 22098-22118/foo I/TestRunner: java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
at 安卓.support.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:189)
at 安卓.support.test.espresso.base.RootViewPicker.findRoot(RootViewPicker.java:134)
at 安卓.support.test.espresso.base.RootViewPicker.get(RootViewPicker.java:80)
at 安卓.support.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:69)
at 安卓.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:23)
at 安卓.support.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:9)
at 安卓.support.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:68)
at 安卓.support.test.espresso.ViewInteraction$2.run(ViewInteraction.java:166)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at 安卓.os.Handler.handleCallback(Handler.java:739)
at 安卓.os.Handler.dispatchMessage(Handler.java:95)
at 安卓.os.Looper.loop(Looper.java:135)
at 安卓.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:696)
01-25 10:22:42.891 22098-22118/foo I/TestRunner: ----- end exception -----
01-25 10:22:42.891 22098-22118/foo I/TestRunner: failed: Test mechanism
01-25 10:22:42.892 22098-22118/foo I/TestRunner: ----- begin exception -----
01-25 10:22:42.892 22098-22118/foo I/TestRunner: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.os.Bundle.putString(java.lang.String, java.lang.String)' on a null object reference
at 安卓.support.test.internal.runner.listener.InstrumentationResultPrinter.reportFailure(InstrumentationResultPrinter.java:183)
at 安卓.support.test.internal.runner.listener.InstrumentationResultPrinter.testFailure(InstrumentationResultPrinter.java:173)
at org.junit.runner.notification.SynchronizedRunListener.testFailure(SynchronizedRunListener.java:63)
at org.junit.runner.notification.RunNotifier$4.notifyListener(RunNotifier.java:142)
at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:72)
at org.junit.runner.notification.RunNotifier.fireTestFailures(RunNotifier.java:138)
at org.junit.runner.notification.RunNotifier.fireTestFailure(RunNotifier.java:132)
at org.junit.internal.runners.model.EachTestNotifier.addFailure(EachTestNotifier.java:23)
at org.junit.runners.ParentRunner.run(ParentRunner.java:369)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at 安卓.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at 安卓.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at 安卓.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1928)
# 1 楼答案
我遇到了同样的问题,因为我测试的是独立片段,而不是活动,我创建了
FragmentTestRule
扩展ActivityTestRule
。我必须在每次测试中调用launchActivity()
方法# 2 楼答案
讨厌看到这个问题没有答案
所以,对于每一个可能在这方面遇到障碍的人:
我的解决方案是在测试类上使用
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
注释,并将第一个测试用例重命名为aaa_my_testcase
见:MethodSorters,FixMethodOrder
# 3 楼答案
我也有同样的问题,只是因为规则, 您可以在规则的构造函数中设置要启动的活动:
最后一个参数负责启动活动
# 4 楼答案
@Rule导致活动在每个单独的@Test之前启动。要在静态@BeforeClass初始化方法中使用Espresso,首先使用ActivityScenario显式启动活动