Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用模拟构造函数参数模拟抽象类?_Java_Spring_Unit Testing_Mocking_Mockito - Fatal编程技术网

Java 用模拟构造函数参数模拟抽象类?

Java 用模拟构造函数参数模拟抽象类?,java,spring,unit-testing,mocking,mockito,Java,Spring,Unit Testing,Mocking,Mockito,我想使用Mockito对一个抽象类进行单元测试,如本文所述 诀窍在于,抽象类依赖于注入其构造函数的策略。我已经创建了该策略的模拟,我希望我的基类模拟实例在单元测试中使用模拟策略 有没有关于如何连接的建议?我目前没有使用任何国际奥委会框架,但正在考虑Spring。也许它会起作用 // abstract class to be tested w/ mock instance abstract BaseClass { // Strategy gets mocked too prote

我想使用Mockito对一个抽象类进行单元测试,如本文所述

诀窍在于,抽象类依赖于注入其构造函数的策略。我已经创建了该策略的模拟,我希望我的基类模拟实例在单元测试中使用模拟策略

有没有关于如何连接的建议?我目前没有使用任何国际奥委会框架,但正在考虑Spring。也许它会起作用

// abstract class to be tested w/ mock instance
abstract BaseClass
{
    // Strategy gets mocked too 
    protected BaseClass( Strategy strategy)
    {
        ...
    }
}
更新

根据Mockito邮件列表,目前还没有将参数传递给mock的构造函数的方法。

我见过在spring上下文级别使用Mockito完成这种事情

例如:



我希望这有帮助

你不需要做任何特别的事情。只需像平常一样模拟bean:

Bean bean = mock(Bean.class); 
when(bean.process()).thenReturn(somethingThatShouldBeNamedVO);

Just works:)

我最终只是使用反射在基类中设置了一个私有字段,如下所示:

// mock the strategy dependency
Strategy strategyMock = mock( Strategy.class);
when(....).thenReturn(...);

// mock the abstract base class
BaseClass baseMock = mock(BaseClass.class, CALLS_REAL_METHODS);

// get the private streategy field
Field strategyField = baseMock.getClass().getSuperclass().getDeclaredField("_privateStrategy");

// make remove final modifier and make field accessible
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(strategyField, strategyField.getModifiers() & ~Modifier.FINAL);          
strategyField.setAccessible(true);

// set the strategy
strategyField.set(baseMock, strategyMock);

// do unit tests with baseMock
...
如果私有字段的名称发生更改,它将崩溃,但它已被注释,我可以接受。这很简单,很简单;这是一行代码,我发现这比在测试中公开任何setter或显式地子类更可取


编辑:所以它不再是一行代码,因为我的私人字段需要是“最终”字段,需要一些额外的反射代码才能使用。

谢谢,Jordan!不幸的是,我认为它不适用于我的案例,因为我需要在策略类中注入特定的模拟行为到基中。如果使用了策略的默认模拟(仅返回null、0等),则模拟基类的逻辑将中断。不过,谢谢!这并没有回答这个问题。很棒的帖子!如果您有一个Spring依赖项,那么您可以用一行代码完成所有这些;-)。在斯普林的糖果盒周围窥探时发现了这个班级<代码>org.springframework.test.util.ReflectionTestUtils.setField(baseMock,“\u privateStrategy”,strategyMock)
// mock the strategy dependency
Strategy strategyMock = mock( Strategy.class);
when(....).thenReturn(...);

// mock the abstract base class
BaseClass baseMock = mock(BaseClass.class, CALLS_REAL_METHODS);

// get the private streategy field
Field strategyField = baseMock.getClass().getSuperclass().getDeclaredField("_privateStrategy");

// make remove final modifier and make field accessible
Field modifiersField = Field.class.getDeclaredField("modifiers");
modifiersField.setAccessible(true);
modifiersField.setInt(strategyField, strategyField.getModifiers() & ~Modifier.FINAL);          
strategyField.setAccessible(true);

// set the strategy
strategyField.set(baseMock, strategyMock);

// do unit tests with baseMock
...