DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Multi-Threaded Geo Web Crawler In Java
  • Yet 4 More Techniques for Writing Better Java
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java

Trending

  • Rethinking Recruitment: A Journey Through Hiring Practices
  • While Performing Dependency Selection, I Avoid the Loss Of Sleep From Node.js Libraries' Dangers
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  • Transforming AI-Driven Data Analytics with DeepSeek: A New Era of Intelligent Insights
  1. DZone
  2. Coding
  3. Java
  4. Mocking the Unmockable: The Mockito2 Way

Mocking the Unmockable: The Mockito2 Way

Mockito2 has made improvements over its predecessor, including the ability to mock final classes. Its new opt-in feature lets you take mockery to a new level.

By 
Saral Khare user avatar
Saral Khare
·
Feb. 03, 17 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
22.6K Views

Join the DZone community and get the full member experience.

Join For Free

Earlier versions of Mockito had the limitations of not being able to mock final classes or static and final methods, etc. However, with Mockito2, the team has introduced an incubating, opt-in feature to take mocking a level ahead. We can now mock final classes using this incubating feature.

I have prepared a HelloWorld example to give a simple demo on how to use this opt-in feature. 

Below is the snapshot of my project structure and the JARs required for this example.

Image title

Key Points

  1. Create a simple file with the name “org.mockito.plugins.MockMaker” and place it in a folder named “mockito-extensions”. This folder should be made available on the classpath. Here, I have added it within the “src” folder so that will also do the job.

  2. The content of the file created above should be a single line: mock-maker-inline.

The above two steps are required in order to activate the Mockito extension mechanism and use this opt-in feature.

The runtime environment should point to a JDK installation and not a JRE, as the native library attach.dll is available in the JDK installation. If this is not done, a runtime exception message will be thrown: “No compatible attachment provider is not available.”

Now let's have a look at the classes shown in the logic “package.”

FinalClass.java:

Image title

Foo.java:

Image title

FooTest.java:

Image title

Output:

Image title

Explanation:

  1. In the 1st assert statement, the real implementation is called hence there is one line printed on the console, and the value returned is “0” so the assertion passes.

  2. In the 2nd assert statement, the mocked implementation is called hence there is no output on the console, and the value returned is “1” so the assertion passes.

And that is how we can use the incubating feature of Mockito2 to mock the final classes.

If you have any questions, feel free to leave a comment. Good luck with Mocking!

Java Development Kit Console (video game CLI) Pass (software) Implementation Java (programming language) Mockito Assertion (software development) career

Published at DZone with permission of Saral Khare. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Multi-Threaded Geo Web Crawler In Java
  • Yet 4 More Techniques for Writing Better Java
  • Injecting Implementations With Jakarta CDI Using Polymorphism
  • Jakarta WebSocket Essentials: A Guide to Full-Duplex Communication in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!