本文介绍了模拟@org.jboss.seam.annotations.in 单元测试的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
测试:
public class BeanTest {
private SomeBean target;
@Test(groups = "integration")
public void checkIfAuthenticationWorks() {
ApplicationBean applicationBean = mock(ApplicationBean.class);
target = new SomeBean();
// Some cool code to inject applicationBean to target class
assertEquals("token", target.authenticate(USERNAME, PASSWORD));
}
}
班级:
@AutoCreate
@Name("someBean")
@Scope(ScopeType.SESSION)
public class someBean implements Serializable {
@Logger
private static Log log;
@In
ApplicationBean applicationBean;
public String authenticate(String username, String password) {
// Very cool code!
return "token";
}
}
是否有一些聪明的方法可以解决 applicationBean 注入部分?
Is there some smart way of solving the applicationBean injection part?
//雅各布
推荐答案
首先,以Seam方式进行测试,即扩展SeamTest
:
First, make the test the Seam way, that is extending SeamTest
:
public class BeanTest extends SeamTest {
private SomeBean target;
@Test(groups = "integration")
public void checkIfAuthenticationWorks() {
target = (SomeBean) Component.getInstance(SomeBean.class);
// target get injected with the MockApplicationBean
assertEquals("token", target.authenticate(USERNAME, PASSWORD));
}
}
然后,创建一个具有 MOCK
优先级的 MockApplicationBean
并将其放入测试类路径中,以便它将被注入以代替真正的 ApplicationBean
:
Then, create a MockApplicationBean
with MOCK
precedence and put it in the test classpath so that it will be injected in place of the real ApplicationBean
:
@Name("applicationBean")
@Install(precedence = MOCK)
public class MockApplicationBean extends ApplicationBean
{
// your mocked ApplicationBean
}
最后,注意target
必须实例化为Seam组件,不能用new":
Finally, note that target
must be instantiated as a Seam component, not with "new":
SomeBean target = (SomeBean) Component.getInstance(SomeBean.class);
这篇关于模拟@org.jboss.seam.annotations.in 单元测试的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!