Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing synchronization

Consider the following method:

/**
 * Set whether messages are printed to System.out.     * 
 * @param printOutput True to print, false for silent logging
 */
public void setPrintOutput(boolean printOutput) {
   // Synchronize to messages because this field is used when a message is received
   synchronized (messages) {
      this.printOutput = printOutput;
   }
}

This method is part of a set of several methods that involve messages, so I want to write a test that checks that this method is synchronized on messages. Does anyone know how I would do this?

like image 517
Michael K Avatar asked Mar 09 '26 04:03

Michael K


1 Answers

I think this is beyond unit testing because the whole point of synchronization is to provide a certain type of connection between two distant pieces of code.

You can test the way it behaves with messages being null and not null, and that gives you full coverage. It won't say anything about whether your synchronization is semantically correct.

like image 65
biziclop Avatar answered Mar 11 '26 16:03

biziclop



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!