I follow your posts with a lot of interest and I have a question for you (your experience could be helpful for me).
I’m the (new) software architect of a J2EE application that uses EJB 2.1 (Entity Bean, Session Bean, and some MDB). This app is linked to a relational database and I’d like to unit test a remote method of a Session Bean which have dependency on the database (could perform some find, and some update).
Do you have an idea on how I could “unit test” this kind of thing ? The aim is to integrate the test into a maven build as an integration-test (I supposed)…
For sure, I cannot do INSERT SQL statement in the setup() method : the datamodel is too big for this kind of approach…
You have a method that has a dependency to yet another class that may perform actions in a database. And you want to test the method. And just to spice the mix somewhat, this method is normally executed in a application container. How could this method be tested without the need for the application container and specially without the need for the database? My answer would be, it is difficult unless you are able to inject your own implementations of the dependencies. But if you can inject you own implementation, or use a mock, then it should be relative easy. You can’t assert the behavior by watching the changes in a database but you can assert that the method that will do things in the database is called properly.
So, my approach would be to instantiate the class with the remote method in. Inject whatever dependencies it need. Call the method you really want to test and assert that the dependencies are called properly. This will require either setters for each dependency or that you can set them through a constructor. I would probably use Mockito to create the fake implementations of the dependencies so I can verify that the interaction has occurred properly.
Verifying that the database changes are done correct is something I would try to avoid from a session bean. It would be better from another test that only verifies the database component, possible using an in memory database to speed up the execution.