Explain Codes LogoExplain Codes Logo

Use Mockito to mock some methods but not others

java
mockito
unit-testing
test-coverage
Anton ShumikhinbyAnton Shumikhin·Aug 22, 2024
TLDR
// Partial Missouri, pardon, Mockito with `spy` MyClass partialMock = spy(new MyClass()); // Don Stub here to return us `someValue` doReturn(someValue).when(partialMock).someMethod(); // Real McCoy for other methods partialMock.realMethod(); // Executes actual method

How partial mocking works: A quick intro

Partial mocking with Mockito plays on the nifty tightrope between reality and fabrication: Real objects are subject to a facelift of sorts—certain methods being replaced with mock behaviors.

But use this sparingly—a heavy reliance on partial mocks might lead to bloated code that's difficult to maintain.

Partial mocks: Uses and abuses

Partial mocks come handy while testing legacy code and unmodifiable third-party interfaces. However, this does not mean you become a 'partial mock junkie'.

A better practice often is to deploy real objects for testing. By doing so, you get to understand the unadulterated system behavior instead of an artificially simulated one.

Stumbling blocks with partial mocks

Partial mocks bestow great power, but with great power comes... Well, you guessed it - great responsibility. These cute little creatures can make your tests crumble like a cookie.

Advanced mock settings: Script your performance better

Mockito's withSettings() serves a fantastic option for 'partial mock connoisseurs':

MyClass partialMock = mock(MyClass.class, withSettings() .defaultAnswer(CALLS_REAL_METHODS) .useConstructor()); // Now get your mock act together for someMethod when(partialMock.someMethod()).thenReturn(someValue); // As for the rest, let's keep it real, alright?

This setting (.defaultAnswer(CALLS_REAL_METHODS)) ensures that your simple instruction of when().thenReturn() takes the centerstage—makes it the lead actor. And, the other, unstubbed agents? They continue to play their original role.

Mastering the partial mock: Step-by-step guide

Fear not—taming this beast isn't all that daunting, provided you keep these pointers in mind. When used judiciously, partial mocks can turn into your go-to sidekicks for efficient testing.

Go real when you can

Keep your tests as grounded as possible—stick to real instances. It doesn't just make your tests sturdy; it also encourages a well-structured and squeaky clean codebase.

Keep it simple, silly

Code should tell a story—a memorable one at that. Enshroud your tests in clarity and simplicity. Nothing should be left to the reader's imagination.

Sift through potential use cases

Partial mocks really shine when you're dealing with external dependencies that aren't always at your beck and call. Take them for a spin when a full-fledged integration test seems unreasonable.

Smell the code

If you smell something, say something. If you're reaching out for partial mocks too often, then perhaps there's something fishy with your code structure... A stitch in time could save you the refactor grind!