Assert.fail()停止runTestOnUiThread()上的所有测试

时间:2015-08-13 17:31:14

标签: android testing junit instrumentation

我有以下问题。我正在使用ActivityInstrumentationTestCase2在我的Android应用上进行一些测试。代码如下:

public class LoginTest extends ActivityInstrumentationTestCase2<LoginActivity> {
  final CountDownLatch signal = new CountDownLatch(1);

  @Before
  public void setUp() throws IOException, JSONException {
    repository = getActivity().getRestAdapter().instance();
  }

  @SmallTest
  public void testNullCredentials() throws InterruptedException{
    runTestOnUiThread(new Runnable() {
      public void run() {
        repository.loginUser(username, password, new LoginCallback() {

          @Override
          public void onSuccess(AccessToken token, User currentUser) {
            Assert.fail("Test was a success. Consumption should fail.");
            signal.countDown();
          }

          @Override
          public void onError(Throwable t) {
            Assert.fail(); // Test should fail here.
            signal.countDown();
          }
        });
      }
    });
    assertTrue(signal.await(20, TimeUnit.SECONDS));
    Assert.assertTrue("Signal", signal.getCount() == 0);
  }
  ... more tests
}

我希望这个测试失败,在调试时,我发现Assert.fail();确实失败了。但是,该类中的所有其他测试都不会运行,因为我得到以下内容:

Test failed to run to completion. Reason: 'Instrumentation run failed due to 'junit.framework.AssertionFailedError''. Check device logcat for details

如何使此测试失败但继续其他测试?谢谢!

编辑1

我也尝试使用getInstrumentation().runOnMainSync()代替runTestOnUiThread()无效...

0 个答案:

没有答案