Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

Does @Mock creates a real object and injects into the variable in Mockito?  RSS feed

 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If @Mock doesn't create a real object then why Singleton pattern is considered bad for mock-testing? I mean if mocking doesn't need a real object then why Singleton pattern matters for the injection of that object?

thanks in advance.
 
author & internet detective
Posts: 39054
714
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun,
You can inject a mock as a singleton. However, the mock then gets used by all the tests. This makes it hard to tell what is happening in the tests. It also makes it harder to specify different behavior for the mock in different places.
 
Arun Singh Raaj
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay but does mocking create a real object?  
 
Jeanne Boyarsky
author & internet detective
Posts: 39054
714
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you mean by a "real" object. It creates a proxy that has different behavior than the one you are trying to replace.
 
Arun Singh Raaj
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read that "Mockito doesn't allow mocking static methods, so Singleton is an anti pattern."

I assume a "mocked" object is a fake object, so if an object is fake then why does it need the class or method to be mocked?

And as you said "proxy" object, may I know what is a proxy object and what is the role of a "real" object in creation of a proxy object?

"real" object I meant the one which directly refers its class, you can access all the data-members of its class with the object, plus if you perform "instanceof" for its class then the result must be true.

Thanks.
 
Jeanne Boyarsky
author & internet detective
Posts: 39054
714
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arun Singh Raaj wrote:I have read that "Mockito doesn't allow mocking static methods, so Singleton is an anti pattern."


Injecting a singleton is a bad idea. However, you don't need to mock a static method to inject a singleton. You do need to write some hacky code.

Arun Singh Raaj wrote:I assume a "mocked" object is a fake object, so if an object is fake then why does it need the class or method to be mocked?


You don't. I think they are referring to the hacky code.

Arun Singh Raaj wrote:And as you said "proxy" object, may I know what is a proxy object and what is the role of a "real" object in creation of a proxy object?

.
Suppose you have interface MyRestCalls. The real object implements the interface with a concrete class that actually makes REST calls. If you wanted, you could manually write a mock object that does anything you want, like returning dummy data. Mockito generates a proxy object where the calls are automatically taken care of. And you set the values you want to be non-defaults on the proxy.
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!