The test:
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));
}
}
The class:
@AutoCreate
@Name("someBean")
@Scope(ScopeType.SESSION)
public class someBean implements Serializable {
@Logger
private static Log log;
@In
ApplicationBean applic开发者_如何学GoationBean;
public String authenticate(String username, String password) {
// Very cool code!
return "token";
}
}
Is there some smart way of solving the applicationBean injection part?
// Jakob
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));
}
}
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
}
Finally, note that target
must be instantiated as a Seam component, not with "new":
SomeBean target = (SomeBean) Component.getInstance(SomeBean.class);
精彩评论