logoalt Hacker News

didipyesterday at 9:18 PM11 repliesview on HN

As someone who is not in the Java world, why does Java need a mocking library? Interface based polymorphism is not enough?


Replies

krackersyesterday at 9:26 PM

Mocks make it easy to record and assert on method invocations. Additionally spys (instance mocks) are really useful when you need to forward to the real method or rely on some state.

At the moment I can't see anything Mokckito gives that you technically couldn't implement yourself via subclassing and overriding, but it'd be a lot of boilerplate to proxy things and record the arguments.

show 1 reply
totallykvotheyesterday at 9:21 PM

Mockito allows one to write mocks in tests for code that doesn't use dependency injection and isn't properly testable in any other way.

On the one hand, you should just design things to be testable from the start. On the other... I'm already working in this codebase with 20 years of legacy untestable design...

show 1 reply
tripple6yesterday at 9:29 PM

Mockito uses declarative matching style of specifying what should be mocked. You don't need to implement or even stub all of interface methods since Mockito can do it itself. It may be extremely concise. For example, interfaces may have tens methods or even more, but only one method is needed (say, java.sql.ResultSet). And finally probably the most important thing, interaction with mocks is recorded and then can be verified if certain methods were invoked with certain arguments.

show 1 reply
marginalia_nuyesterday at 9:25 PM

The point is to let you create mocks without having to go through the whole polymorphism rigmarole, without forcing classes to define a separate interface or anything like that.

eoskxyesterday at 9:25 PM

As someone who has been out of Java for close to 10 years now, you certainly could do without Mockito, but you'd be writing a lot of boiler plate code repetitively. There's also the case of third-party libraries that you don't control and Mockito has decent facilities for working with those, especially when you're working with a codebase that isn't pure DI and interfaces.

davnicwilyesterday at 9:57 PM

because even supposing you have an interface for your thing under test (which you don't necessarily, nor do you necessarily want to have to) it lets you skip over having to do any fake implementations, have loads of variations of said fake implementations, have that code live somewhere, etc etc.

Instead your mocks are all just inline in the test code: ephemeral, basically declarative therefore readily readable & grokable without too much diversion, and easily changed.

A really good usecase for Java's 'Reflection' feature.

show 1 reply
gleennyesterday at 9:21 PM

There are many cases where you don't control the library code your code depends on that you want to test. Also, the FactoryFactoryFactory patterns can be quite cumbersome and simply mocking out something makes for a far simpler test. There are likely more common cases.

throwaway7375yesterday at 10:58 PM

Before Mockito, it was common (where I worked) to create an interface just to support testing. This is an anti-pattern in my opinion. To create interfaces just for testing complicates the code and it is one of my pet peeves. It also encourages the factory pattern.

I prefer Mockito's approach.

voidhorseyesterday at 9:20 PM

Arguably it doesn't. Mocking is over used and you should just use a real implementation or distributor provided fake whenever possible.

wiseowiseyesterday at 10:32 PM

It doesn't. But good luck teaching hordes of enterprise "developers".

wetpawsyesterday at 9:20 PM

[dead]