Best current framework for unit testing EJB3 / JPA [closed]
Asked Answered
D

5

21

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?

Dipsomaniac answered 21/3, 2010 at 2:26 Comment(0)
T
11

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

Tedford answered 23/3, 2010 at 7:48 Comment(0)
A
8

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.

Audit answered 24/3, 2010 at 5:44 Comment(1)
I would classify testing a single EJB against an embedded container as also being a unit test. IMO integration testing is more using Selenium etc.Waterworks
T
3

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.

Tap answered 24/3, 2010 at 5:29 Comment(0)
T
1

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.

Thissa answered 8/4, 2010 at 13:40 Comment(0)
E
1

I would suggest Arquillian. With Arquillian you have the options to test normal EJBs, or CDI beans. You could run your test agains an enbedded, managed, or remote EE container. The Arquillian project is continuously maintanced...

However it is not always so easy to make your first arquillian test run. You might have problem finding the dependencies for the correct arquillian version and container version. I have posted a tutorial serial about this. But to not make me suspious of promoting, the link is not posted here. If you like you could just google "Tutorial EJB3 Integration Test with Arquillian part1".

Enclave answered 30/1, 2013 at 12:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.