Live data from Hacker News

Stepping down as Mockito maintainer after ten years

github.com

31–40 of 220 posts

Re: Stepping down as Mockito maintainer after ten years

#31
> To me, it felt like the feature was presented as a done deal because of security.

Not security, but integrity, although security (which is the #1 concern of companies relying on a platform responsible for trillions of dollars) is certainly one of the primary motivations for integrity (others being performance, backward compatibility or "evolvability", and correctness). Integrity is the ability of code to locally declare its reliance on some invariant - e.g. that a certain class must not be extended, that a method can only be called by other methods in the same class, or that a field cannot be reassigned after being assigned in the constructor - and have the platform guarantee that the invariant is preserved globally throughout the lifetime of the program, no matter what other code does. What we call "memory safety" is an example of some invariants that have integrity.

This is obviously important for security as it significantly reduces the blast radius of a vulnerability (some attacks that can be done in JS or Python cannot be done in Java), but it's also important for performance, as the compiler needs to know that certain optimisations preserve meaning. E.g. strings cannot be constant-folded if they can't be relied upon to be truly immutable. It's also important for backward-compatibility or "evolvability", as libraries cannot depend on internals that are not explicitly exposed as public APIs; libraries doing that was the cause of the migration pain from Java 8 to 9+, as lots of libraries depended on internal, non-API methods that have changed when the JDK's evolution started picking up steam.

In Java, we've adopted a policy we call Integrity by Default (https://openjdk.org/jeps/8305968), which means that code in one component can violate invariants established by code in another component only if the application is made aware of it and allows it. What isn't allowed is for a library - which could be some fourth-level dependency - to decide for itself at some point during the program's execution, without the application's knowledge, that actually strings in this program should be mutable. We were, and are, open to any ideas as long as this principle is preserved.

Authors of components that do want to do such things find the policy inconvenient because their consumers need to do something extra that isn't required when using normal libraries. But this is a classic case of different users having conflicting requirements. No matter what you do, someone will be inconvenienced. We, the maintainers of the JDK, have opted for a solution that we believe minimises the pain and risk overall, when integrated over all users: Integrity is on by default, and components that wish to break it need an explicit configuration option to allow that.

> built on a solid foundation with ByteBuddy

ByteBuddy's author acknowledges that at least some aspects of ByteBuddy - and in particular the self-loading agent that Mockito used - weren't really a solid foundation, but now it should be: https://youtu.be/AzfhxgkBL9s?t=1843. We are grateful to Rafael for explaining his needs to us so that we could find a way to satisfy them without violating Integrity by Default.

Re: Stepping down as Mockito maintainer after ten years

#32
post #3
post #2

For those, like me, who haven't heard of it: Mockito is the "most popular mocking framework for Java".

It’s taken years off of my life dealing with the test mess people have made with it.

Absolutely the worst. 1000 line test setups that shatter into pieces the instant you try to make the simplest change to a function. Makes refactoring an absolute nightmare.

Re: Stepping down as Mockito maintainer after ten years

#33
post #4

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

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 & grok…

An anonymous inner class is also ephemeral, declarative, inline, capable of extending as well as implementing, and readily readable. What it isn't is terse.

Mocking's killer feature is the ability to partially implement/extend by having some default that makes some sense in a testing situation and is easily instantiable without calling a super constructor.

Magicmock in python is the single best mocking library though, too many times have I really wanted mockito to also default to returning a mock instead of null.

Re: Stepping down as Mockito maintainer after ten years

#34

Earlier quoted context omitted.

Actually, no. "small booger" would be _moquito_ in spanish.

Fair. The spelling is off, but the pronunciation is the same.

I'm Spanish and subconsciously pronounced the library as MOCKito, as opposed to moQUIto.

Re: Stepping down as Mockito maintainer after ten years

#35

What does Agent mean in this context? And what is "dynamic attachment of agents"?

Tools for profiling, debugging, monitoring, thread analysis, and test coverage analysis can attach to the Java Virtual Machine (JVM) using the 'Tool Interface'

If you've got a running java process on your local machine right now, you can use 'jconsole' to see the stack traces of all threads, inspect various memory statistics, trigger an immediate garbage collection or heap dump, and so on. And of course, if the tool is an instrumenting profiler - it needs the power to modify the running code, to insert its instrumentation. Obviously you need certain permissions on the host to do this - just like attaching gdb to a running process.

This capability is used not just by for profiling, debugging and instrumentation but also by mockito to do its thing.

Java 21 introduced a warning [1] saying this will be disabled in a forthcoming version, unless the process is started with '-XX:+EnableDynamicAgentLoading' - whereas previously it was enabled by default and '-XX:+DisableAttachMechanism' was used to disable it.

The goal of doing this is "platform integrity" - preventing the attachment of debugging tools is useful in applications like DRM.

[1] https://openjdk.org/jeps/451

Re: Stepping down as Mockito maintainer after ten years

#37

Earlier quoted context omitted.

It also translates to “small booger”, in Spanish, which always made me question who thought the name was a good idea over there.

Actually, no. "small booger" would be _moquito_ in spanish.

Look, as an English only speaker I don't care - I'm still stuck at "Haw haw, small booger library!"

Re: Stepping down as Mockito maintainer after ten years

#38

Earlier quoted context omitted.

your test environment should not have the credentials to write to prod data. yiiiiikes!

Credentials end up existing in prod because the person used Mochito and didn’t override the function for providing credentials :’c

Credentials should only be provided at the application root, which is going to be a different root for a test harness.

Mockito shouldn't change whether or not this is possible; the code shouldn't have the prod creds (or any external resource references) hard coded in the compiled bytecode.

Re: Stepping down as Mockito maintainer after ten years

#39
This is a good opportunity to ditch mocking and use fakes with adapters. Not only mocks create brittle tests that often test only the framework itself, but they do so in an order of magnitude slower way.

Also, F Kotlin and their approach of "we'll reinvent the wheel with slightly different syntax and call it a new thing". Good riddance I say, let them implement their mockk, or whatever it is called, with ridiculous "fluent" syntax.

Re: Stepping down as Mockito maintainer after ten years

#40
post #32
post #3

Earlier quoted context omitted.

It’s taken years off of my life dealing with the test mess people have made with it.

Absolutely the worst. 1000 line test setups that shatter into pieces the instant you try to make the simplest change to a function. Makes refactoring an absolute nightmare.

What's specifically bad about Mockito here? Poor defaults for mocks?
Post reply on HN