• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the way to do in Java unit testing what one does by Patch and MagicMock in Python unit tests

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In python unit testing, unit tests are written with help of Patch and MagicMock . Reference: https://docs.python.org/3/library/unittest.mock.html What is the way in Java unit tests to do what is dong using Patch and to do what is done using Magic Mock of python? Thanks
 
Saloon Keeper
Posts: 27707
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are several different Unit Testing and Mocking platforms for Java.

JUnit is popular for unit testing. In a Maven project, you'd put the test classes under the project's /src/test/java directory so that they would not be included in production builds.

Mockito is one of several Mock frameworks in common use.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Mockito is one of several Mock frameworks in common use.



Yes, in Mockito, what I am trying to understand is that  what is the way to do the part that we can do using patch in Python unit tests and what is the way to do the part that one can do using MagicMock in Python unit tests (does Mock() of Java do the same or anything else)?
 
Tim Holloway
Saloon Keeper
Posts: 27707
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Somewhere on the Ranch, I believe I published about 6 "gotchas" for mocking in Java. I wish I could remember where.

But the #1 rule was never mock if you had a better alternative.

Mockito's mocking doesn't simulate the mocked objects as much as it simply makes a "black hole" for the rest of the code to plug into. Unless you go to a lot of effort, the mocked methods do nothing and return nothing, null, or zero. And it's even worst if you need to mock something stateful.

In Java, a better approach is to define an Interface and a simulated implementation of that Interface, then wire it in in place of the production implementation, This works especially well when using a wiring framework such as Spring.

You cannot always do that, of course, and sometimes you do need something like Mockito, but again, it's a last resort for me.
 
Marshal
Posts: 8842
631
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:
Yes, in Mockito, what I am trying to understand is that  what is the way to do the part that we can do using patch in Python unit tests and what is the way to do the part that one can do using MagicMock in Python unit tests (does Mock() of Java do the same or anything else)?



It is hard to answer to this question, unless you know exactly how both frameworks work, what are their similarities and differences.

You better tell, what do you want to do with Java in your unit tests, and then we might help you how to achieve that.

But briefly looking at those you mentioned for Python, I think Mockito does all that, or at least to most extent.
 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really familiar with Python's patch options, but perhaps Mockito's equivalent is a spy. You take a normal object, and Mockito wraps it for you. Unless you tell Mockito to replace method calls it will call the actual methods. And a bonus is that you can verify that calls were made afterwards - even for non-mocked methods.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I will read on spy in Mockito.
 
Tim Holloway
Saloon Keeper
Posts: 27707
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fair warning, though. Java 9 changed how introspection works, and that impacted Mockito a lot. Since the Internet is great at serving up stale data, make sure that anything you read is up to date.

The original spy mechanism was also pretty cumbersome but it's much more streamlined now,
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic