简体   繁体   English

为什么我们不能使用Mockito为Parameterized Constructor创建间谍

[英]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. 我想间谍参数化构造函数注入模拟对象作为我的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?. 但是我们有什么东西可以在构造函数中注入模拟对象并窥探它吗?

You can do that by instantiating your main class with parametrized constructor in your junit and then creating a spy from it. 您可以通过在junit中使用参数化构造函数实例化主类,然后从中创建一个间谍来实现。

Let's suppose your main class is A . 我们假设你的主要班级是A Where B and C are its dependencies 其中BC是它的依赖关系

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 然后,您可以使用参数化类为此编写junit,如下所示

@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. 这里BC是使用参数化构造函数在类A注入的模拟对象。
  2. Then we created a spy of A called spyA . 然后我们创建了A名为spyAspy
  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 . 我们通过修改A类中受保护方法method2的返回值来检查spy是否真的有效,如果spyA不是A的实际spyA是不可能的。

It sounds like you might be missing a dependency injection solution. 听起来你可能错过了依赖注入解决方案。 Mockito is great for working with your DI to inject mocks. Mockito非常适合与你的DI一起注射嘲笑。 For example, you can use CDI, annotation your Notification and EntryService members with @Inject , declare @Mock s for both in your test, and then let Mockito inject those into your RegDao for testing. 例如,您可以使用CDI,使用@Inject注释NotificationEntryService成员,在测试中为两者声明@Mock s,然后让Mockito将这些注入您的RegDao进行测试。

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM