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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With