How do you unit test Java EE code?
Asked Answered
M

4

25

I want to ask for your prefered way to test Java EE code?

I found only three project, that are trying to help to code unit tests in Java EE environment:

So I wonder,

  • is there any framework helping to write (j) unit test for Java EE code?
  • do you use embedded Java EE servers like jboss or glassfish v3?
  • do you mockup and inject by yourself?

Thanks a lot...

Megalomania answered 22/12, 2009 at 17:17 Comment(3)
See #1734305Reno
Antoher out of date project: sourceforge.net/projects/mockejb Last Release: 2004-09-04Megalomania
There's this article by Adam Bien about unit testing for JavaEE from 2011.Evergreen
U
24

If by Unit Testing you mean... unit testing (testing a unit in isolation), then you actually don't need any particular framework since EJB3.0 are nothing more than annotated POJOs and thus can be relatively easily tested without any special fixture.

Now, if you mean something else - like Integration Testing or Functional Testing - then, yes, tools can help and simplify things (but you should really start to use the right terminology :) I'll assume that this is what you have in mind.

First, JUnitEE seems dead and obsolete and I'm not even sure it has anything for EJB3.x. Second, I'm not impressed by the Java EE 5 support of Cactus and having to deploy Cactus tests is painful (I think that Cactus was nice for J2EE 1.4 but is a bit outdated now). So this leaves us with Ejb3Unit which is in my opinion the best option, especially if you want to run out of container tests i.e. without really deploying the application (much faster).

If you want to run in container tests, then you could indeed use an embedded container and my current preference goes to GlassFish v3, even for Java EE 5 (I may be wrong but I'm pretty disappointed by the starting time of the latest JBoss releases so it isn't getting much of my attention). See the post GlassFish Embedded Reloaded, an appserver in your pocket for sample code (that you could use from your tests) or Using maven plugin for v3 embedded glassfish (if you are using maven).

Another option would be to package and deploy your application with Cargo and then run some tests against the deployed application (with Selenium or a BDD tool for example). This could be useful if you want to run end-to-end tests with a container that doesn't provide any embedded API.

So, to answer your last question, I would indeed use available tools, maybe a combination of them, for tests that are not unit tests and wouldn't mock/inject stuff myself, except if they don't cover some needs that I can't think of right now.

Unsupportable answered 22/12, 2009 at 22:57 Comment(6)
I heard Cactus project is back to life with new lead developer Petar Tahchiev. JSFUnit is based on Cactus.Godwit
@Godwit Oh, good to know! Actually, I was maybe a bit too much focused on EJB3 and totally forgot JSF. Thank you very much for this information.Unsupportable
Thanks a lot. I think, I will create cunstructors for all my EJB pojos to allow injection without any reflection. Cargo ist intresting for integration test. Thanks!Megalomania
Is there any easy Mocking/Simulating for JMS, TimerService and SessionContext?Megalomania
"and thus can be relatively easily tested without any special fixture": but not EJBs that have a @PersistenceContext EntityManager?Scofflaw
@PascalThivent: "2011/08/05 - Jakarta Cactus has been retired."Evergreen
T
2

As you are interested in unit testing, I recommend JUnit. You can unit test the methods in the core classes. If you have difficulty in writing unit test cases using JUnit, then probably the design is not modular and it is highly coupled. First focus on your core functionality and test it using JUnit.

Tomkins answered 23/12, 2009 at 6:53 Comment(0)
A
2

I've been facing the same problem of running integration tests based on JUnit in a Java EE 6 container (Glassfish v3, to be precise), and after a lot of browsing and searching, I could not find a solution that really suited me needs, so I wrote my own, now published as jeeunit on Google Code.

I wouldn't call it a test framework, it is really just a handful of classes providing the glue between JUnit and Embedded Glassfish.

The general idea is similar to Cactus, your tests run in the container and get triggered by a servlet from outside.

jeeunit supports JUnit 4, Glassfish v3, CDI and generates the standard XML JUnit reports just like Ant or Maven Surefire (in fact, I reused some code from Ant for generating the reports).

Adaliah answered 11/5, 2010 at 18:40 Comment(0)
L
2

I had a requirement to test a CDI application and wrote a custom JUnit runner that runs everything outside of the web container.

http://jglue.org/cdi-unit/

It is suitable for Java SE and also supports dummy Request, Session and Conversation scopes for testing web apps.

It's small and fast, which is great when you have lots of unit tests.

Limburg answered 18/11, 2011 at 23:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.