Reputation:
I'm using IBM RAD 7 (aka Eclipse 3.4) and WebSphere 7.
I have an EJB project that contains an @Stateless EntityService
and an @Stateless EntityDAO
and so on.
I have an Web project that contains a JAX-RS restful web service that looks up the EntityService with this JNDI URL:
ejblocal:entityEAR/entityEJB.jar/[email protected]
That all works great.
My question is, what would be the "correct" way to write JUnit tests to test the EntityService
and EntityDAO
classes?
Since the system needs to be running in the WebLogic server to function, I thought I would get the app running, then launch the JUnit test which does a look up of the same JNDI that the web service is using, but I get an error:
Naming Manager ... getURLContext cannot find the factory for this scheme: ejbLocal
Any suggestions are useful, how should I approach writing JUnit tests?
Upvotes: 2
Views: 3912
Reputation:
At the end of the day, I implemented a @Remote remote interface for the Service classes I wanted to test, and did a remote JNDI lookup to the service bean.
More details here:
how to write a JUnit test that can see my EJB service?
Upvotes: 0
Reputation: 9149
In general, JUnit is intended to write unit tests. Within unit test you verify work of a single component having single responsibility (at least it should have single responsibility :))- all dependencies are mocked somehow (easymock, mockito and so on).
Dependency injection used in EJB simplifies this process - you can instantiate bean in your unit test setUp()
method using new
operator (you don't need container) and then, inject mocked dependencies (the same way container injects real dependencies).
This is the approach I use. The other thing are integration tests, which verifies entire scenario - starting from webservice (or other remote facade method) call, through beans logic, up to the DB queries. However, in such case you don't verify components standing after webservice/facade. Just webservice output for the specific input.
The good approach is to write test first (failing at the beginning) and then, write the implementation to satisfy it. For unit tests (single bean, tests not run within container) I'd recommend JUnit and EasyMock/Mockito. For integration tests you can use Selenium or JUnit+OpenEJB as a form of simple container for tests (especially if you have remote facade in a form of EJB component). Also, using tools like SoapUI you can create entire tests scenarios for your webservice - post some data, get them, modify, put, againg get, delete and so on.
Upvotes: 0
Reputation: 7243
If you're writing Unit Tests, then they shouldn't depend on the container (because they execute only in a JVM) so you can't do JNDI lookups in them. To test your EJB Beans and DAO's with JUnit a Mocking Framework (like EasyMock) can be a great help.
But if you're interested in testing the communication between your EJB's and your REST Services, then you need Integration Tests and I doubt JUnit can help you here. A popular tool for Integration Tests is Selenium, and you need a fully-functional container and enviroment for your tests to execute.
Upvotes: 1