Starting a new project using EJB 3 / JPA, mainly stateless session beans and batch jobs. I've used JUnit in the past on standard Java webapps and it seemed to work pretty well. In EJB2 unit testing was a pain and required a running container such as JBoss to make the calls into. Now that we're going to be working in EJB3 / JPA I'd like to know what companies are using to write and run these tests. Are Junit and JMock still considered relevant or are there other newer frameworks that have come around that we should investigate?
IMHO, yes they are still relevant.
With EJB3, you can test you EJB either like regular POJO, or as managed bean using an embedded EJB container.
Same for JPA, you can either embed a JPA implementation for your test and use an in-memory database, or you can mock the data layer completely.
For my last EJB project I had written a bit of glue code to test the EJB because embedded EJB container were not mature enough, but now I would go for an embedded EJB container + JUnit.
A few resources
But there are many others easily findable
For unit testing, you can use JUnit, TestNG and your favorite mocking framework (EasyMock, Mockito, PowerMock, JMockit, JMock).
For integration testing, you could start/stop an embeddable container (JBoss, OpenEJB, GlassFish v3) from your tests. Or, have a look at Arquillian, a very recent new player for integration testing of Java EE applications:
The mission of the Arquillian project is to provide a simple test harness that developers can use to produce a broad range of integration tests for their Java applications (most likely enterprise applications). A test case may be executed within the container, deployed alongside the code under test, or by coordinating with the container, acting as a client to the deployed code.
To avoid introducing unnecessary complexity into the developer's build environment, Arquillian integrates transparently with familiar testing frameworks (e.g. JUnit 4, TestNG 5), allowing tests to be launched using existing IDE, Ant and Maven test plugins without any add-ons.
Also check Improving the Testability of Java EE With Arquillian 1.0.0 Alpha 1 Released and the Arquillan space. Looks interesting IMO.
You can go for Apache OpenEJB which is a lightweight EJB3.0 implementation that can be embedded into TestNG.
Here you can find sample code which uses OpenEJB, TestNG and jMockIt for unit testing.
One option is also to use Spring, but of course your architecture will then be based on Spring on the runtime as well. The reason I like Spring is that it is easy to inject what ever mock data and objects is needed during the testing phase as you can annotate in the JUnit test case which context file to use:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:test-context.xml"})
public class MyClassTest {
@Autowired
private MyClass myClass;
@Test
public void testMyMethod() {....}
But if you don't want to use Spring anyway, then this is quite irrelevant :) On the other hand, it is very easy to switch out from Spring afterwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With