Today I needed to mock some methods on a Java static class for unit testing. I used PowerMockito because, as far as I can tell, Mockito cannot mock static classes, but the two tools have very similar functionality. I used when, then , and thenCallRealMethod to run other code and then return the normal result, but found that a ClassCastException was thrown in the mocked method. The stack trace didn't provide a line number for that frame, and the argument types were correct in the calling code, so I was confused.
I had accidentally used lambda syntax to return something from the then handler that was not of the method's return type. When PowerMockito tried to use that as the return value, the exception was generated. Apparently chaining thenCallRealMethod after then is ignored.
No comments:
Post a Comment