I have a class called NullMetricsPublisher
, there are a few methods inside, for example one of them void publish()
. As the class name already indicates that the class should doing nothing. The void publish()
method is essentially empty.
void publish() {
// do nothing in this method
}
However, we do want to unit test it, make sure no logic inside the method at all. Wondering anyone know how to test an empty method of a class in Java?
Note: Why we have NullMetricsPublisher
in first place? We do have another class called MetricsPublisher
, which publishes metrics to our metrics service. However, in some cases, we don't want to publish metrics to at all. The existing interface must require a Publisher
, therefore, now we just added NullMetricsPublisher
class to implement Publisher
.
You cannot test that a method does nothing with a unit test. In order to test this you would need to check that nothing that could happen, does happen. There are endless things that could happen. Therefore it is not possible to write a unit test for this.
The only thing you could test is that specific actions don't happen, e.g. the NullMetricsPublisher
doesn't send messages like the MetricsPublisher
.
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