gpt4 book ai didi

java - 为什么我的 stub 方法返回 null?

转载 作者:搜寻专家 更新时间:2023-11-01 02:23:34 25 4
gpt4 key购买 nike

我有一个类 Dummy。我注入(inject)了 3 个变量。但是,其中一个是不可注入(inject)的,因为它是一个接口(interface)。所以我注入(inject)了一个对象,其中一个方法返回所需的类型。

Class Dummy
{
private final Class1 class1;
private final Class2 class2
private final Interface1 interface1;

@Inject
Dummy(Class1 class1, Class2 class2, HelperClass helperclass)
{
this.class1 = class1;
this.class2 = class2;
this.interface1 = helperclass.somefunction();
}
}

HelperClasssomefunction 返回Interface1 的一个实例。

这是我的测试:

@RunWith(MockitoJUnitRunner.class)
Class DummyTest
{
@Mock
private Class1 class1;

@Mock
private Class2 class2;

@Mock
private HelperClass helperclass;

@InjectMocks
private Dummy dummy;

@Before
public void start()
{
Interface1 mockInterface = mock(Interface1.class);
when(helperclass.somefunction()).thenReturn(mockInterface);
}
@Test
public void test()
{
// etc...
}
}

但是,当我运行测试时,interface1 为空。我做错了什么?

最佳答案

@InjectMocks 发生在 @Before 注释之前。

出于这个原因(和 other reasons ),我建议根本不要使用@InjectMocks;只需构建您的 SUT @Before 方法中的类,带有真正的构造函数。

当您向测试类中添加一些打印语句时,这种顺序是显而易见的。我删除了所有 Class1Class2 内容,因为它们不相关。看到这段代码运行:

@RunWith(MockitoJUnitRunner.class)
public class DummyTest {
@Mock
private HelperClass helperclass;

@InjectMocks
private Dummy dummy;

@Before
public void start()
{
System.out.println("In @Before!");
Interface1 mockInterface = mock(Interface1.class);
when(helperclass.somefunction()).thenReturn(mockInterface);
}

@Test
public void test()
{
System.out.println("In @Test!");
}

public static class Dummy {
public final Interface1 interface1;
public final HelperClass helperclass;

@Inject
Dummy(HelperClass helperclass)
{
System.out.println("In dummy constructor!");
this.interface1 = helperclass.somefunction();
this.helperclass = helperclass;
}
}

private static class HelperClass {
Interface1 somefunction() {
return new Interface1() {};
}
}

private static interface Interface1 {

}
}

输出:

In dummy constructor!
In @Before!
In @Test!

如果您坚持使用 @Mock@InjectMocks 来做,您可以尝试使用 answer 参数:

@Mock(answer=Answers.RETURNS_MOCKS)

将使 helperclass.somemethod() 返回一个 mock 而不是 null


老实说,它以这种方式工作并不奇怪。 Mockito 的作者真的不喜欢 Partial Mocks/Stubs,并明确表示 in their documentation :

As usual you are going to read the partial mock warning: Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy objects. How does partial mock fit into this paradigm? Well, it just doesn't... Partial mock usually means that the complexity has been moved to a different method on the same object. In most cases, this is not the way you want to design your application.

However, there are rare cases when partial mocks come handy: dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.) However, I wouldn't use partial mocks for new, test-driven & well-designed code.

helperclass 返回 null 以外的东西是部分模拟,因此他们不会喜欢它。

关于java - 为什么我的 stub 方法返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32053589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com