Android检测运行失败 - 断言错误

时间:2015-10-28 17:06:24

标签: java android junit

我正在努力弄清楚为什么我的测试用例无法完成运行。我感到很困惑,因为错误似乎只表明断言失败了。这意味着测试失败,但为什么会导致整个测试运行器崩溃?

我在控制台中看到此错误。

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

这是追溯:

10-27 19:51:29.006    4188-4188/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.jsonapi.sample, PID: 4188
junit.framework.AssertionFailedError
        at junit.framework.Assert.fail(Assert.java:48)
        at junit.framework.Assert.assertTrue(Assert.java:20)
        at junit.framework.Assert.assertNotNull(Assert.java:218)
        at junit.framework.Assert.assertNotNull(Assert.java:211)
        at com.example.jsonapi.JsonAPITest$2.onDone(JsonAPITest.java:101)
        at com.example.jsonapi.JsonAPITest$2.onDone(JsonAPITest.java:96)
        at org.jdeferred.impl.AbstractPromise.triggerDone(AbstractPromise.java:107)
        at org.jdeferred.impl.AbstractPromise.triggerDone(AbstractPromise.java:98)
        at org.jdeferred.impl.DeferredObject.resolve(DeferredObject.java:70)
        at com.example.jsonapi.JsonAPI$1.onResponse(JsonAPI.java:178)
        at com.example.jsonapi.JsonAPI$1.onResponse(JsonAPI.java:172)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:67)
        at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5257)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

以下是相应的测试:

public void testFindOneCustomURL() throws Exception {
    ArrayList<String> reqList = new ArrayList<>();
    reqList.add("queue.txt");

    MockWebServer server = getServerWithData(reqList);
    // Ask the server for its URL. You'll need this to make HTTP requests.
    URL baseUrl = server.getUrl("/api/v1");

    JsonAPI client = new JsonAPI(baseUrl, context);
    client.getGsonBuilder()
           .setDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    client.register(new VideoQueue());
    Promise prom = client.find(new VideoQueue())
            .set("user", "212139")
            .run()
            .done(new DoneCallback<ArrayList<VideoQueue>>() {
                @Override
                public void onDone(ArrayList<VideoQueue>  videoQueue) {
                    assertNotNull(videoQueue);
                    assertNotNull(videoQueue.get(0));
                    assertNotNull(videoQueue.get(0).getId());
                    assertEquals("3310190", videoQueue.get(0).getId());
                }
            }).fail(new FailCallback() {
                @Override
                public void onFail(Object rejection) {
                    assertNull(rejection);
                }
            });

    prom.waitSafely();
    // Shut down the server. Instances cannot be reused.
    server.shutdown();

    // Make sure our client hit the server
    RecordedRequest request = server.takeRequest();
    assertEquals("/api/v1/users/212139/queue", request.getPath());
}

1 个答案:

答案 0 :(得分:1)

这意味着断言失败了。通常它应该报告测试用例失败。但是如果你把断言放在一个回调方法中,它似乎会崩溃。也许这是junit的错误。