Mocking dependencies in unit tests using JMockIt

Ein weiterer Blogpost aus dem Firmenblog, wieder in Englisch.

Garage & GarageTest

This test case demonstrate the use of JMockIt and it's Expectations to resolve dependencies in unit tests. Expectations are a way to easily mock the behavior of an object in unit tests, without creating interfaces, classes and other overhead. Take a look at the full unit test first, and I'll explain what's done there later. You can find the interface Garage and the classes CheapGarage and EngineFactory at the end of this blogpost.

Now, back to the method testRepairWithExpectationEngineFactory(). The EngineFactory is a dependency buried down in the CheapGarage class that is responsible for replacing the Engine in a Car. There is a mocked instance of EngineFactory in this unit test that will return a new DieselEngine every time the method instantiate() is called with any kind of argument. This way I can control exactly how the hidden call to a factory behaves. JMockIt has a bunch of other useful features and I recommend it for all unit tests. If you care about your tests and try to avoid testing dependencies instead of your logic, then you should start using JMockIt today. :-)

Weitere Artikel

Gronkh ist jetzt ein Rundfunksender...

Crossfit Regionals 2018 in Berlin

Crossfit Open 2018

Qutebrowser userscript for TT-RSS

Aufbau eines Hackintosh

Enable request logging in Jetty 9.1

Hacktoberfest 2017

Automatische Erneuerung von Lets Encrypt Zertifikaten

Neue Antennen für meine Fritzbox

Fitness vs Gesundheit