expected=JSONException.class 没有捕捉到

expected=JSONException.class doesn't catch

我做了一个测试,我发送了一个无效的 JSON,它应该会收到一个 JSONException.

当我 运行 测试时,它失败并显示 JSONException,为什么测试没有捕捉到它?

@Test (expected = JSONException.class)
public void testExtraFieldsJsonException() {
    String json = "wrong_data";
    fragment.decodeJson(json);
}

我用org.json.JSONException.

测试结果。

org.json.JSONException: Value wrong_data of type java.lang.String cannot be converted to JSONObject
    at org.json.JSON.typeMismatch(JSON.java:111)
    at org.json.JSONObject.<init>(JSONObject.java:158)
    at org.json.JSONObject.<init>(JSONObject.java:171)
    at co.some.mainactivities.ProfileFragment.extraFields(ProfileFragment.java:154)
    at co.some.mainactivities.ProfileFragment.callExtraFields(ProfileFragment.java:243)
    at co.some.mainactivities.TestProfileFragment.testExtraFieldsJsonException(TestProfileFragment.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:250)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)

它在 new JSONObject(json);

中失败
private void decodeJson(String json) {
    try {
     JSONObject json = new JSONObject(json);
     int fieldsQuantity = json.getJSONObject("extra_fields").length();
     //....
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}

这是预期的行为。但是测试用例应该通过。它失败了吗?

您正在 decodeJSON 中捕获异常,因此它没有传播到测试。

如果您希望将异常抛给 decodeJSON 的调用者,例如您的测试,请删除 try-catch 并将 throws JSONException 添加到函数签名。