测试:
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注入部分的聪明方法?
// Jakob
答案 0 :(得分:1)
首先,使测试成为Seam方式,即扩展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));
}
}
然后,创建一个MockApplicationBean
优先级为MOCK
并将其放入测试类路径中,以便将其注入以代替真实的ApplicationBean
:
@Name("applicationBean")
@Install(precedence = MOCK)
public class MockApplicationBean extends ApplicationBean
{
// your mocked ApplicationBean
}
最后,请注意target
必须实例化为Seam组件,而不是“new”:
SomeBean target = (SomeBean) Component.getInstance(SomeBean.class);