为什么我们不能使用 Mockito 为参数化构造函数创建间谍
why cannot we create spy for Parameterized Constructor using Mockito
我的代码中只有参数化构造函数,我需要通过它注入。
我想监视参数化构造函数以注入模拟对象作为我的 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);
但是我们有什么东西可以在构造函数中注入模拟对象并监视它吗?
听起来您可能缺少依赖注入解决方案。 Mockito 非常适合与您的 DI 一起注入模拟。例如,您可以使用 CDI,将 Notification
和 EntryService
成员注释为 @Inject
,在您的测试中为两者声明 @Mock
,然后让 Mockito 将它们注入您的RegDao
用于测试。
这是我认为您正在尝试的测试的工作模型 运行:
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());
}
}
您可以通过在您的 junit 中使用参数化构造函数实例化您的主 class 然后从中创建一个间谍来做到这一点。
假设您的主要 class 是 A
。其中 B
和 C
是其依赖项
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;
}
}
然后你可以使用参数化的class为此编写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();
}
}
测试输出class
A's method called
20
- 此处
B
和 C
是您使用参数化构造函数注入 class A
的模拟对象。
- 然后我们创建了
spy
个 A
,名为 spyA
。
- 我们通过修改 class
A
中受保护方法 method2
的 return 值来检查 spy
是否真的有效如果 spyA
不是 A
的实际 spy
,则可能。
我的代码中只有参数化构造函数,我需要通过它注入。
我想监视参数化构造函数以注入模拟对象作为我的 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);
但是我们有什么东西可以在构造函数中注入模拟对象并监视它吗?
听起来您可能缺少依赖注入解决方案。 Mockito 非常适合与您的 DI 一起注入模拟。例如,您可以使用 CDI,将 Notification
和 EntryService
成员注释为 @Inject
,在您的测试中为两者声明 @Mock
,然后让 Mockito 将它们注入您的RegDao
用于测试。
这是我认为您正在尝试的测试的工作模型 运行:
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());
}
}
您可以通过在您的 junit 中使用参数化构造函数实例化您的主 class 然后从中创建一个间谍来做到这一点。
假设您的主要 class 是 A
。其中 B
和 C
是其依赖项
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;
}
}
然后你可以使用参数化的class为此编写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();
}
}
测试输出class
A's method called
20
- 此处
B
和C
是您使用参数化构造函数注入 classA
的模拟对象。 - 然后我们创建了
spy
个A
,名为spyA
。 - 我们通过修改 class
A
中受保护方法method2
的 return 值来检查spy
是否真的有效如果spyA
不是A
的实际spy
,则可能。