justastefan
justastefan

Reputation: 595

How to mock annotated EJBs?

I googled it already, but seems kinda hard to find topics on mocking dependency 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.)

Upvotes: 4

Views: 2193

Answers (2)

Arjan Tijms
Arjan Tijms

Reputation: 38163

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.

Upvotes: 1

anergy
anergy

Reputation: 1384

May be have a look at Mockito. You can the apply @EJB annotation on a setter and inject mocked LoginService in your tests.

Upvotes: 4

Related Questions