Tag Archives: JUnit

TestNG revamped support for JUnit

I have spent a couple of hours this weekend on a new approach to running JUnit 3.x tests in TestNG

Previously, we have tried to emulate through TestNG the behavior of JUnit and things were quite complex. Also, considering how many extensions are there for JUnit we have never been sure we are able to run all existing JUnit tests.

So, I decided to give it another try and see if I can come out with a better approach. And instead of trying to emulate JUnit runtime behavior, I thought I can handle JUnit tests to JUnit itself and just make it report back the results. After a couple of hours of hacking I got it working and right now I think we will be able to run an even bigger percentage of existing JUnit code. And as a validation of the changes, I have run not only the TestNG tests (almost 300), but also JUnit internal tests (131) and all passed.

This new revamped support for JUnit will be released in the next version, but if you want to challenge it please feel free to build it yourself from the SVN trunk (or drop me an email) and put it to extensive work.

4 Comments

Filed under Uncategorized

More on JMock and TestNG

More on JMock and TestNG
Firstly, I would like to provide a small fix to the code provided in the previous entry: JMock for TestNG (or JUnit-free JMock)

/**
* Verify the expected behavior for the mocks registered by the current thread and
* also releases them.
*/
public static synchronized void verifyAndRelease() {
   Thread currentThread= Thread.currentThread();
   List mocks = s_mockObjects.get(currentThread);
   if(null != mocks) {
       try {
           for(Verifiable verifiable : mocks) {
               verifiable.verify();
           }
       finally {
           // make sure the mocks are released
           mocks.clear();
           s_mockObjects.put(currentThread, null);
       }
   }
}

The fix assures that even if the verification of a mock fails, the current mocks are released and will not remain around till the next test.

I have started to completely free JMock of its JUnit “addiction”. I have found three types of dependencies:

  • classes in the hierarchy of org.junit.TestCase (3). These can be completely replaced by the class I have already posted
  • usage of org.junit.AssertionFailureException. Having in mind that TestNG works for JVM 1.4+, we have directly used the java.lang.AssertError, and I am thinking to do the same here
  • a couple of classes extending org.junit.Assert (even if they aren’t using much of it). Considering that TestNG propose a more readable form for assertions through org.testng.Assert, this change is quite trivial

I can confess that these changes are already available on my machine and I am already using them. Me and Joe have been discussing about the possibility to integrate these changes directly in JMock. If this will not happen, than I will most probably include the code in TestNG.

A last point (one that I just get me burnt tonite), is that JMock doesn’t check (or I haven’t figured how to make it) the order of expectations, so from its perspective the following code is same correct (or wrong):

   [...]
   mock.expect(once()).method("methodOne");
   mock.expect(once()).methog("methodTwo");
   [...]
   mock.verify();

and

   [...]
   mock.expect(once()).method("methodTwo");
   mock.expect(once()).methog("methodOne");
   [...]
   mock.verify();

will both pass or fail disregarding the real order of invocation. I agree that in most of the cases, JMock behavior is enough good, but not having a way to force the order of invocation is in my opinion a missing feature. I am gonna try to add this feature to my code.

2 Comments

Filed under Uncategorized

JUnit release and state of TestNG

Two weeks ago JUnit has announced the new version. I was waiting for this release for quite a long time… but not because of the features of JUnit 4, because most of them (or all) have been available in TestNG a long time before. In a previous post I have done some comparisons of JUnit 4 features with TestNG features (and Cedric did it too: JUnit4 Overview). So, my waiting was not about the feature list, but rather to see the reactions of people in the testing space. And it looks like my expectations were meet. I just have a few links about reactions, and here there are:

More interesting posts about TestNG can be found here.

Enjoy your tests with TestNG!

category: , ,

Leave a comment

Filed under Uncategorized

JUnit 4.0

Read post JUnit 4.0.

3 Comments

Filed under Uncategorized

TestNG and IDE support

During the last months, while developing TestNG, I have read and also received lots of comments about the lack of support for TestNG in the most used IDEs (Eclipse, IntelliJ IDEA and NetBeans). I have put some of my spare time into developing a plugin for Eclipse (by the way it is working great, and the features are quite nice [blink/]). I’ve had intentions to do the same for Idea, but I cannot see how I can develop a plugin with the Plugin Development Package.
Why? That 14Megs download contains 45Megs javadocs, which are mostly empty. I don’t have any document describing which way to go. And those 6 examples are little toys. While the Eclipse API is really huge, I was able to find valid javadoc comments, lots of examples and even some good books (not to mention the fact that developing with Eclipse means learning SWT/JFace). I also know about the support site, but I usually go to this kind of places to ask important/specific questions, and not <<Please help…>> ones.
I keep my interest open to support TestNG in Idea, but for the moment this seems to me quite impossible.

Disclaimer: this post is by no means meant to upset IntelliJ IDEA‘s developers. It is only my short experience while trying to develop a plugin.

2 Comments

Filed under Uncategorized

A new step for TestNG

A few minutes ago we have announced the new version of TestNG.
The list of new features is published on Cedric’s announcement.

I must mention the new types of @Configurations: before/afterSuite and before/afterTest. I intend to post a nice interaction of TestNG and DBUnit based on these.

The almost completely rewritten Ant task is bringing into scenes some important properties (attributes) that will allow you to use TestNG on daily builds and/or continuous integration.

Another thing that must be mentioned (big thanks to Hani and Patrick) is that TestNG has joined OpenSymphony (so we have a new forum, wiki and JIRA).

We hope that the experience is even better now and we are looking to hearing from you.

Leave a comment

Filed under Uncategorized

JUnit in your pocket

There are almost 2 weeks from the last post, but it seems that this end of year will be a very busy one, so I will have little time to write. I hope I’ll be back more active on the next year.
Now, getting away from this type of excuses and back to the topic.
Thanks to JavaRanch I had the opportunity to read the last Kent Beck’s book on JUnit: JUnit – Pocket Guide.
As the name states, it is indeed a pocket guide: 77 pages, from which almost 20 are about JUnit usage inside IDEs (if the editor would cut these full of images pages too and reduce a little bit the format, the book would enter your pocket without any problems [smile/]). Otherwise, the book contains a little of everything: history, reasoning behind creating and using JUnit, a short presentation of the framework architecture and implementation, reduced JUnit API comments and very few examples. Some short comments on possible extensions and that’s all. I think everything inside is in pocket suited format.
Concluding, I would say that JUnit – Pocket Guide is an (very) entry level reading for those wanting to have the first contact with JUnit. More infos are to be found on the JUnit site and other books.
Next reading: JUnit Recipes a book recommended everywhere and with great reviews.
Update: in the pocket review above I’ve forgot to pass the idea I loved the most:

[…]However, if I’m working by myself, I find it helpful to leave the last test broken at the end of the day. When I arrive in the morning, I know just what to do to get started: fix that test. That’s usually enough to get me started on my day

1 Comment

Filed under Uncategorized