Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay EasyMock verification

I'm using EasyMock to create mock objects for JUnit testing in Java. I create a mock object and pass it to another thread where it expects methods to be called. In the other thread, the calls are enclosed in a try/catch(Throwable) block, so when an unexpected call occurs on the mock and it thus throws AssertionError, that error is caught by the catch block and treated. So, even though an unexpected call occurred, the test passes.

In order to have the test fail as expected, I would like to delay all verification of calls to the EasyMock.verify(mock) call made in the test-runner thread at the end. Is this possible and how?

like image 955
Philipp Avatar asked Mar 08 '26 00:03

Philipp


2 Answers

I'm not sure about how to do this with EasyMock, but this behavior is possible with Mockito because verification assertions can be specified at the end of the test.

like image 100
deterb Avatar answered Mar 09 '26 12:03

deterb


The correct solution I'd guess is to stop catching Throwable. Doing so catches all Errors as you're finding, which can be quite dangerous... are you absolutely positively 100% sure you need to catch Throwable? Why?

(If it turns out you do, you could catch AssertionError specifically and rethrow it. But that's ugly!)

like image 38
Steven Schlansker Avatar answered Mar 09 '26 13:03

Steven Schlansker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!