Here are my initial thoughts (which I posted as a forum comment elsewhere)
Layer 0. Test the entity beans are deployable (You'll need some of the framework from Layer 4 for this). Basically, you need to know that all your annotations work. Things to watch out for are multiple @Id fields in one class or @EmbeddedID or @IdClass in conjunction with @ManyToOne, @ManyToMany, @OneToMany, @OneToOne and fun with @JoinTable, @JoinColumn and @JoinColumns. Once you know how these are supposed to work with the specification, it's not too bad to write it correctly each time. But there are some gotchas that will break things later on.
Layer 1. Do the functions in the classes that don't depend on annotations work as expected. Typically, this is just going to be the getters and setters in your entity classes. Of course JUnit best practice says we don't bother testing functions that look like:
public T getX() {
return this.x;
}
or
public void setX(T x) {
this.x = x;
}
as there is nothing that can go wrong with them. So in that case, your level 1 tests will just be initial values specified from constructors and verifying that the non-get/set pairs work, and that the getters you have tagged @Transient work (because you've likely put some logic in them)
Layer 2. Test the session bean methods that don't require injection to work.
Layer 3. Test the session bean methods that require injection (Mock Objects). Simulate the injection for yourself, injecting Mock Objects for the entity manager. Then you can confirm that the correct methods are being called in the correct sequences, etc.
[Note this may require some skill in designing the mock. I'm working on developing my own entitymanager mock, and if it looks useful I'll release it to the world.
Layer 4. Test the session bean methods that require injection (Real entity manager) (See Layer 0)
For this you will need an out of container persistence implementation. Currently Hibernate and Glassfish provide beta versions. You will need a different persistence.xml file that lists all the entities. You will have to use reflection to inject the entity manager(s) that you create from an entity manager factory unless you provide a constructor that takes an EntityManager as a parameter. You may need to use reflection to call any @PostConstruct method if you made it private.
Layer 5. Navigate the relationships in the objects returned from Layer 4 using a database that has been loaded with test data.
I am currently using Layers 0, 1, 2 & 4 to test my session beans and entity beans.
Has anyone else any other ideas?
Ejb3Unit - Out-of-Container EJB 3.0 Try out Ejb3Unit. http://ejb3unit.sourceforge.net/ The Ejb3Unit project automates Entity and Session bean testing outside the container for the EJB 3.0 specification. Ejb3Unit can execute automated standalone JUnit tests for all EJB 3.0 conform J2EE projects. The out of container test approach leads to short build-test-cycles, because no container deployment is necessary anymore.
ReplyDeleteI tried Ejb3Unit out, it's great. Thanks Daniel for the info. Do you know if there are also ather example project, i found only http://ejb3unit.sourceforge.net/SampleProject.html
ReplyDeleteJohn
Ejb3Unit 2.0 is comming!!! - with (http://ejb3unit.sourceforge.net/Roadmap.html):
ReplyDelete* Improvement: No dependencies - on jar thats it!! (only log4j.jar and optional jmock.jar [for mocked tests] will be required). This will ease the installation and development in large projects because less dependencies are required. No jar file conflicts anymore!
* New Feature: Support for relational CSV data loaders. Load your test data with references!
* New Feature: Support for peristence.xml no need to specify your persistence objects for every test!
* Improvement: Completely refactored EntityManger life cycle handling - will speed up your testing!
* Improvement: Support of the new jMock2 framework!
* Improvement: More feedback for failing test cases!
* Improvement: Broken entities (e.g. syntax error in query) will not break other tests anymore!
* Lots of bug fixes!
Hello, I would like to try EJB3Unit. I want at first to do an automated test for an entity bean to get a feeling how it works. It seems quite simple but I didn't understand how to start the test. I don't jave any experience with maven and I tried to install the maven-eclipse plugin but i cannot find it. Could anybody shows me how it goes? Thank you very much.
ReplyDeleteHatem
I tried to use EJB3Unit but unfortunately I find it hard to got the first steps. The installation documentation is not clear so the pom will have some errors. The example project does not fit the current release Classes ...
ReplyDeleteDoes anyone have a good HowTo to learn handling EJB3Unit?
Is the project still in progress or did ist stop? The last Release is 2.0.0-RC-1 an was published over 1 year ago :|
I have the feeling the Ejb3Unit project is running dead. No release > 1 year, developers absent in the forums, not reacting to everything. Maybe its time for a fork and put the project to a new lead.
ReplyDelete