why cannot we create spy for Parameterized Constructor using Mockito

徘徊边缘 提交于 2021-02-06 09:42:30

问题


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?.


回答1:


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.



回答2:


It sounds like you might be missing a dependency injection solution. Mockito is great for working with your DI to inject mocks. For example, you can use CDI, annotation your Notification and EntryService members with @Inject, declare @Mocks for both in your test, and then let Mockito inject those into your RegDao for testing.

Here's a working mockup of the test I think you're trying to run:

import static org.junit.Assert.assertEquals;

import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MockitoSpyInjection {
    static class Notification { }
    static class EntryService { }
    static class RegDao {
        @Inject
        Notification foo;

        @Inject
        EntryService  bar;

        public RegDao() {
        }

        public RegDao(Notification foo, EntryService bar) {
            this.foo = foo;
            this.bar = bar;
        }

        public Notification getFoo() {
            return foo;
        }

        public EntryService getBar() {
            return bar;
        }

    }


    @Mock
    Notification foo;

    @Mock
    EntryService bar;

    @Spy
    @InjectMocks
    RegDao dao;

    @Test
    public void test() {
        assertEquals(foo, dao.getFoo());
        assertEquals(bar, dao.getBar());
    }
}


来源:https://stackoverflow.com/questions/45514907/why-cannot-we-create-spy-for-parameterized-constructor-using-mockito

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!