Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit Test Empty Method

Tags:

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.

like image 787
Simon Guo Avatar asked Feb 08 '17 00:02

Simon Guo


1 Answers

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.

like image 186
Stefan Birkner Avatar answered Sep 23 '22 10:09

Stefan Birkner