java - In a JUnit test, is there a way I can ensure that all assertions have been executed? -


i have producer , consumer. producer writes messages synchronously. consumer thread polls messages every second.

i have test this:

@test public void shouldconsumemessagewhenmessageisproduced() {     final message expectedmessage = new message("test");     //consumer poll every 1 second message     consumer.poll((actualmessage) -> {assertthat(actualmessage), is(expectedmessage));     producer.sendsynchronously(expectedmessage);     thread.sleep(3000);       } 

this test works. however, there no way me ensure assertion invoked.

i realize use mockito, realize more of integration test unit test. there anyway in junit ensure assertions have been executed?

please note since assertion in lambda, cannot increment variable or set flag.

it seems want test wait until assertion triggered, time out. countdownlatch job:

@test public void shouldconsumemessagewhenmessageisproduced() {     final message expectedmessage = new message("test");      countdownlatch messagereceived = new countdownlatch(1);      //consumer poll every 1 second message     consumer.poll(actualmessage -> {       assertthat(actualmessage, is(expectedmessage));       messagereceived.countdown();     }      producer.sendsynchronously(expectedmessage);      //wait until message received, not more 1 second     //await returns false if reaches timeout     asserttrue(messagereceived.await(1, seconds)); } 

if expect assertion in consumer triggered n times, can change initial count of latch: countdownlatch latch = new countdownlatch(n);.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -