Last week Christian Guedemann has released on OpenNTF a new project, JUnit testing for XPages, I think a missing part of the Domino Designer experience, especially with the increasing use of Java in XPages apps.
So this weekend I did some basic Unit testing.
Test Driven Development
The last 10 month I did most of the time ‘real’ Java development. At a customer I stepped in the Enterprise Java Development, and all the tooling around this type of Development. One of these things was Test Driven Development (TDD), write unit tests before actually writing code.
TDD follows roughly a cycle as shown below.
[dropshadowbox align=”none” effect=”lifted-both” width=”600px” height=”” background_color=”#ffffff” border_width=”1″ border_color=”#dddddd” ][/dropshadowbox]
It require also a other way of development. Like single responsibility of methods. For Unit testing in Java there are also great tooling, Mock Frameworks, like Mockito, EasyMock and PowerMock. These frameworks simulate objects and can be used to match the expected result.
A small warning
On for hand I should warn you, I am not that good in writing Unit tests. I have a lot to learn And writing tests after writing code is not the way.
OK, the testing.
After all the downloading and installing of the plugin and put the demo database on my Domino server. I was ready to go.
The demo database provides a very good way who to use the plugin, see the blog of Christian.
So I created a Test class, to test if a Document could be found by providing a document UNID.
[dropshadowbox align=”none” effect=”lifted-both” width=”600px” height=”” background_color=”#ffffff” border_width=”1″ border_color=”#dddddd” ][/dropshadowbox]
So far so good.
Another small test was giving me a challenge. The method I wanted to test was accepting a Document object. So I added a Mock Framework.
First I tried Mockito, but I ran into problems by initializing the the Mockito Annotations, returning me a nasty stack trace.
So I switched to EasyMock. Added it to my lib directory of my database and added it to the build path.
Now I was able to simulate a Document and set the class to be tested as the TestSubject.
[dropshadowbox align=”none” effect=”lifted-both” width=”600px” height=”” background_color=”#ffffff” border_width=”1″ border_color=”#dddddd” ][/dropshadowbox]
As you can see, nothing special, mostly because the code was already written, and I was not willing to change it.
Conclusion.
The plugin is working very easily. Adding new classes to your JUnit plugin was very easy. One of the things I was missing, and I already discussed it with Christian, was a Mock Framework. When it will be added to the plugin it will add more value to the plugin. Because you don’t need to add a Mock Framework by yourself to the database.
Like Ryan Baxter also mentioned in the discussing, add also PowerMock, which will nicely work together with EasyMock. PowerMock let you test also static methods and objects.