Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why cannot we create spy for Parameterized Constructor using Mockito

I have only parameterized constructor in my code and i need to inject through it.

I want to spy parameterized constructor to inject mock object as dependency for my junit.

public RegDao(){  //original object instantiation here Notification .... EntryService ..... }  public RegDao(Notification notification , EntryService entry) {  // initialize here }  we have something like below :  RegDao dao = Mockito.spy(RegDao.class); 

But do we have something that i can inject mocked object in the Constructor and spy it?.

like image 480
Pradeep Avatar asked Aug 04 '17 20:08

Pradeep


People also ask

When should a spy be used in Mockito?

A Mockito spy is a partial mock. We can mock a part of the object by stubbing few methods, while real method invocations will be used for the other. By saying so, we can conclude that calling a method on a spy will invoke the actual method, unless we explicitly stub the method, and therefore the term partial mock.

How do you make a spy in Mockito?

Creating Spy using @Spy Annotation We can also create a spy object by using the @Spy annotation. To enable Mockito annotations, we can either use the MockitoAnnotations. initMocks(this) method. Or we can use the built-in runner @RunWith(MockitoJUnitRunner.

What is the difference between spy and mock in Mockito?

Mocks are used to create fully mock or dummy objects. It is mainly used in large test suites. Spies are used for creating partial or half mock objects. Like mock, spies are also used in large test suites.

Why Spy is used in Mockito?

Simply put, the API is Mockito. spy() to spy on a real object. This will allow us to call all the normal methods of the object while still tracking every interaction, just as we would with a mock. Note how the real method add() is actually called and how the size of spyList becomes 2.


1 Answers

You can do that by instantiating your main class with parametrized constructor in your junit and then creating a spy from it.

Let's suppose your main class is A. Where B and C are its dependencies

public class A {      private B b;      private C c;      public A(B b,C c)     {         this.b=b;         this.c=c;     }      void method() {         System.out.println("A's method called");         b.method();         c.method();         System.out.println(method2());      }      protected int method2() {         return 10;     } } 

Then you can write junit for this using your parametrized class as below

@RunWith(MockitoJUnitRunner.class) public class ATest {      A a;      @Mock     B b;      @Mock     C c;      @Test     public void test() {         a=new A(b, c);         A spyA=Mockito.spy(a);          doReturn(20).when(spyA).method2();          spyA.method();     } } 

Output of test class

A's method called 20 
  1. Here B and C are mocked object that you injected in your class A using parametrized constructor.
  2. Then we created a spy of A called spyA.
  3. We checked if spy is really working by modifying the return value of a protected method method2 in class A which could not have been possible if spyA was not an actual spy of A.
like image 197
Dhawal Kapil Avatar answered Oct 15 '22 10:10

Dhawal Kapil