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

Der letzte PR

Fahrrad geklaut

Erstes Review des Sony XZ Premium

Unterschiedliche Sichtweisen

Vorbestellt: Sony Xperia XZ Premium

Giants Run 2017

Gelesen: SciFi Serie 'The Expanse'

Neue Tastatur: Pok3r Vortex RGB

New minecraft survival mod for 1.11.2

Änderungen beim Flug mit Quadkoptern