我想使用Mockito创建一个模拟,通过以下方式为一个模拟对象提供两个接口:
Mockito.mock(InterfaceA.class, Mockito.withSettings().extraInterfaces(InterfaceB.class));
我正在使用xml spring配置,我想找到一种方法来使用SPel声明该模拟。 我尝试了以下但没有成功:
<bean id="myMockObject" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="my.package.InterfaceA"/>
<constructor-arg value="#{T(org.mockito.Mockito).withSettings().extraInterfaces(T(my.package.InterfaceB))}"/>
</bean>
看起来spring不能将模拟器自动装配到InterfaceB类型的字段中。有人可以建议我错在哪里吗?
答案 0 :(得分:1)
这对我来说很好;你做意识到你必须使用模拟来访问其他界面方法,不是吗?
<table>
<thead> <th></th></thead>
<tbody>
<c:foreach items="${projectlist}" var="project">
<tr> <td> <input type="text" id="name_**rowindex**" /></td></tr>
</c:foreach>
</tbody>
</table>
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class Foo {
@Autowired
private Bar barr;
@Test
public void foo() {
Bar bar = mock(Bar.class, Mockito.withSettings().extraInterfaces(Baz.class));
((Baz) bar).baz();
((Baz) this.barr).baz();
}
public interface Bar {
void bar();
}
public interface Baz {
String baz();
}
}
来自javadocs ......
<bean id="bar" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="foo.Foo$Bar"/>
<constructor-arg value="#{T(org.mockito.Mockito).withSettings().extraInterfaces(T(foo.Foo$Baz))}"/>
</bean>
编辑:
我刚刚更新了我的测试,我可以通过第二个接口ok ...
注入bean * <p>
* This mysterious feature should be used very occasionally.
* The object under test should know exactly its collaborators & dependencies.
* If you happen to use it often than please make sure you are really producing simple, clean & readable code.
...
* //now, the mock implements extra interfaces, so following casting is possible:
...