VOOZH about

URL: https://dzone.com/articles/mocking-the-unmockable-the-mockito2-way

โ‡ฑ Mocking the Unmockable: The Mockito2 Way


Related

  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 Feb. 03, 17 ยท Tutorial
Likes
Comment
Save
23.2K 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: 

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
  • A Spring Boot App With Half the Startup Time
  • Alternative Structured Concurrency
  • Optimizing Java Applications for Arm64 in the Cloud

Partner Resources

ร—

Comments

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

Let's be friends: