2

Which mocking framework is preferred for testing EJB's (specifically Message Driven Beans), I'm looking at MockObjects, easyMock, MockEJB and mockito but open all.

I've not used mocking framework before and particularly interested in low learning curve / great getting started guide.

I should clarify, using EJB 2.

scottyab
  • 23,621
  • 16
  • 94
  • 105
  • possible duplicate of [What's the best mock framework for Java?](http://stackoverflow.com/questions/22697/whats-the-best-mock-framework-for-java) – dogbane Jan 25 '11 at 11:34
  • 1
    @dogbane - I'm thinking specifically for EJB junit tests – scottyab Jan 25 '11 at 11:37

3 Answers3

4

A framework specifically build for this is Arquillian - http://www.jboss.org/arquillian.

This lets you test in-container via JUnit. You can test individual beans or larger collections. You can provide mocks by simply packaging other implementations in the test archive.

When using Java EE 6 (JBoss AS 6, Glassfish V3), you can run the container in embedded mode which simplifies matters and saves you in run-time overhead.

EJB2 is notoriously difficult to test though. If possible I suggest you drop it completely in favor of EJB3.x, but of course this might not be an option for you.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
  • Not really a mocking (or stubbing) solution, but a different approach to the problem of testing EJBs. Quite probably a better approach! – Tom Anderson Feb 23 '12 at 11:58
2

If you're using EJB 3.x, then is heavily interface-based, and so your usual run-of-the-mill mocking frameworks will do just fine for unit tests; the fact that the code is used by the EJB framework isn't really relevant.

EJB 2.x is a different (and ugly) kettle of fish, though, since it doesn't conform to any of the usual sane rules of software design. Your best bet there is probably a framework like Cactus.

skaffman
  • 398,947
  • 96
  • 818
  • 769
2

It's not a mocking framework, but you might like to have a look at OpenEJB, which is a lightweight EJB container suitable for, amongst other things, use in unit tests. It will save you having to mock the container's interfaces, and you can still mock interfaces to other components.

scottyab
  • 23,621
  • 16
  • 94
  • 105
Tom Anderson
  • 46,189
  • 17
  • 92
  • 133