简体   繁体   English

使用JUnit 4.11无法捕获带有ExpectedException的UnrecognizedPropertyException

[英]Cannot catch the UnrecognizedPropertyException with ExpectedException using JUnit 4.11

I wrote the following code : 我写了以下代码:

    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Test
    public void testMappingException() {


        exception.expect(ArithmeticException.class);

        int x = 1 / 0;
    }

Everything works correctly. 一切正常。 Unfortunatelly it is not working correctly in this case: 不幸的是,在这种情况下,它无法正常工作:

ObjectMapper mapper = new ObjectMapper();
void add(String json) {

    try {

        X x = mapper.readValue(json, X.class); // exception here

    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

======================================================== ================================================== ======

@Before
public void setUpBeforeClass() {
    jsonTestExcpetion = "{\"aaa\" : \"bbb\"}";
}


@Test
public void testMappingException() {

    MyClass mc = new MyClass();
    exception.expect(UnrecognizedPropertyException.class);
    myClass.add(jsonTestExcpetion);

}

It should catch the exception 它应该捕获异常

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException

but in fact, exception is thrown normally and tests fails. 但实际上,异常通常会引发并且测试失败。

Have you got any ideas? 你有什么主意吗?

The exception is catched inside add(String) . add(String)内部捕获了异常。 Therefore JUnit doesn't see it and cannot test it. 因此,JUnit无法看到它并且无法对其进行测试。 (JUnit sees only exceptions that are thrown by the add method.) (JUnit仅查看add方法引发的异常。)

How to test it? 怎么测试呢? There is only one side effect of this method that leaks outside of the method: it writes something to System.err . 此方法只有一个副作用会泄漏到该方法之外:它向System.err写入一些内容。 You can test this using the System Rules library. 您可以使用系统规则库对此进行测试。

But, usually it is bad practice to silently catch exceptions. 但是,通常静默捕获异常是不好的做法。 The caller does not know that an exception occured. 调用者不知道发生了异常。 I suggest to not catching the exceptions: 我建议不要捕获异常:

void add(String json) throws JsonParseException, JsonMappingException, IOException {
    X x = mapper.readValue(json, X.class);
    return null;
}

Then your test will work. 然后您的测试将起作用。

You can wrap the exceptions with a runtime exception, if you don't like the throws in the method's signature. 如果您不喜欢方法签名中的throws ,则可以使用运行时异常包装这些异常。

void add(String json) {
  try {
    X x = mapper.readValue(json, X.class);
    return null;
  } catch (JsonParseException e) {
    throw new RuntimeException(e);
  } catch (JsonMappingException e) {
    throw new RuntimeException(e);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM