Monday, February 17, 2014

No Runnable Methods while running junit test cases

This happens when you have more than one class in unit test case suite. JUnit tries to run all the classes in the suite. But it may happen that some of the classes are already used by main class.
Example:
public class MainTestClass {
HelperClass hClass = new HelperClass(..);
hClass.doStuff();
}

public class HelperClass {
public HelperClass(..) {..}
doStuff(..) {..}
}

In this case, JUnit would try to run both of these classes, and then after running first class, it would throw exception for second class saying 'No Runnable methods' or sometimes it even says - Test class should have exactly one public zero argument constructor.

To solve this, annotate the second class with @Ignore so that JUnit does not try to run it separately. It also makes sense to do so because, second helper class is already being used by first class MainTestClass.
so finally:
import org.junit.Ignore
@Ignore
public class HelperClass {
.....
}

should fix the issue.