Thursday, November 26, 2009

Junit testing

After a long break, recently I came across junit testing. This was when I was helping my team member in writing junit tests and suggested where to start from. Then, there was an issue in running a TestSuite.

The code that I used before is something like below:

import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests {

public static Test suite() {
TestSuite suite = new TestSuite("Test for something");
suite.addTestSuite(SomeClass.class);
return suite;
}
}


But, when i tried to run, I got below error:
java.lang.Exception: No runnable methods

By the way, this is with junit-4. So, here is what we figured out -
In junit-4, they use annotations for TestSuite. So, the old syntax is not working. Here is working syntax

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses( { SomeClass.class})

public class AllTests {
}

So simple. Isn't it?
Also, download junit coverage plugin for eclipse using this update site http://update.eclemma.org/ . More details are here regarding eclemma

No comments: