How to mock annotated EJBs?
I googled it already, but seems kinda hard to find topics on mocking depe开发者_运维问答ndency injected objects (EJB 3.0).
public class MyTestBean
{
@EJB
ILoginService mLoginService;
public void doLogin() {
if (!mLoginService.login(name, pass)) {
// fehler
}
}
When running tests with openEJB I want to have that LoginService.login(name, pass) return true. Is there a way to mock the LoginService bean?
(Currently the login-methode uses some JAAS-stuff i can't emulate during tests.)
May be have a look at Mockito. You can the apply @EJB annotation on a setter and inject mocked LoginService in your tests.
Another option is to simply provide a second implementation of your ILoginService interface. This second implementation is the mock, but no special Mock library or support is necessary for this.
You put this implementation is a special source folder, typically called test. Then you build your deployment scripts so that for normal builds this test folder is ignored. When you do your unit tests and build the archive for testing, you explicitly include the Mock implementation from the test source folder.
精彩评论