Mockito 5.0.0 Released |
Written by Nikos Vaggalis | |||
Thursday, 26 January 2023 | |||
The venerable Java testing framework which allows the creation of test double objects in automated unit tests for the purpose of test-driven or behavior-driven development is going through a major update. Let's check what it offers. When it comes to testing Java applications, there the two top testing libraries: JUnit and Mockito. While you write and execute your unit tests in JUnit, you need Mockito too to mock the costly dependencies used in your tests. These dependencies might be a database or a RESTful API; Mockito uses the constructs of stubs, mocks, and spies to do its job in replacing them. Typically you use Mockito to generate fake classes for the dependencies and then define their responses when their methods get called. Then you do your testing by making sure that the objects you are currently testing interact as expected with the those mocked out classes. Mockito is a mature library which after many iterations has reached version 5. It introduces some major changes starting with the version of Java it supports. Java 8 won't cut it anymore. Now the minimum level is 11. What if you cant' upgrade? For JDK 8 and below, you can keep on using Mockito 4. This is similar to if you are using JDK 6, for which you can keep on using Mockito 2. Code in Java 8 still runs under versions 1 to 4. The changes in Mockito 5 (for now) are primarily focused on the latest JDK versions, which means the API differences between Mockito 4 and 5 are minimal. Because there were incompatibilities with recent versions of the JDK due to the library's usage of JVM-internal API, most notably with the current subclass mockmaker, the switch was made from the default mockmaker to mockito-inline. As such mockito-inline which is used for mocking static methods, has found its way on to the core therefore you don't have to explicitly link to it from your build file in Maven or Gradle when moving your code. Still the maintainers will be supporting the subclass mockmaker because there are still legitimate cases. And finally a new method called type() was added to the ArgumentMatcher interface. This interface allows for customizing Mockito by defining a custom matcher, which you can pass into method arguments to provide more targeted matches. But the major shortcoming of the ArgumentMatcher was the lack of varargs support. Notice that this new method is not a source-breaking change, but is a bytecode-breaking change, as such all code working on Mockito 4 should work as-is when recompiled with Mockito 5.
More InformationRelated ArticlesCheck your Java with Error Prone GraalVM's Alignment With OpenJDK Signifies A New Era For Java Google Joins Adoptium - What's The Deal?
To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info |