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
but in fact, exception is thrown normally and tests fails.
Have you got any ideas?