我发生了一个烦人的问题,其中一个工具测试类显示为灰色,当我开始所有测试(包androidTest)时,这个被省略(其他人开始就好了)。任何想法可能是什么原因?
@RunWith(AndroidJUnit4.class)
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
@Rule
public ActivityTestRule<LoginActivity> mActivityRule =
new ActivityTestRule<>(LoginActivity.class, true, false);
private MockWebServer server;
private LoginActivity loginActivity;
private SharedPreferences loginDetails;
public LoginActivityTest() {
super(LoginActivity.class);
}
Instrumentation.ActivityMonitor giftListActivityMonitor;
@Before
public void setUp() throws Exception {
super.setUp();
// Create a MockWebServer. These are lean enough that you can create a new
// instance for every unit test.
server = new MockWebServer();
// Start the server.
server.start();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
loginDetails = getInstrumentation().getTargetContext().getSharedPreferences("login_details",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = loginDetails.edit();
editor.clear();
editor.commit();
KupMiConstans.BASE_URL = server.url("/").toString();
loginActivity = this.getActivity();
Instrumentation instrumentation = getInstrumentation();
giftListActivityMonitor = instrumentation.addMonitor(GiftListActivity.class.getName(), null, false);
new PrepareSingletons(getInstrumentation().getContext());
}
@Test
...
@Test
...
@After
public void tearDown() throws Exception {
SharedPreferences loginDetails = getInstrumentation().getTargetContext().getSharedPreferences("login_details",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = loginDetails.edit();
editor.clear();
editor.commit();
try {
server.shutdown();
} catch (Exception e) {
}
}
提前致谢!
答案 0 :(得分:0)
显示灰色图标,因为类中的第一件事必须是默认构造函数。在第一次测试完全基于浓缩咖啡并且至少需要一个断言*之前,测试中省略了类。奇怪的是订单如此重要......