Jobin Jose Jobin Jose - 7 months ago 18
Java Question

Do i need to prefix 'test' to every unit test method i create with GWTTestCase?

I am learning how to perform unit testing in GWT. So i create a class which extends the GWTTestCase class and write test methods inside it.

The problem is that if i don't prefix the word 'test' to even one function in this class(public or private) apart from the inherited 'getModuleName' method, i get an 'initialization error'.

Now am i doing something wrong or is it a rule to add this prefix? If its actually a rule, what is the reason for this?

public class hellotest extends GWTTestCase{
@Override
public String getModuleName() {
// TODO Auto-generated method stub
return "hellopackage.Hello1";
}

public void test()
{
assertTrue(true);
}

public void test2()
{
assertTrue(true);
}

public void notTest3() //Produces initialization error
{
assertTrue(true);
}


}

Answer

GWTTestCase extends JUnit 3 TestCase, and in JUnit 3, test methods are indeed supposed to start with test.

The reason is that it's how JUnit has been designed: it searches for public void methods starting with test in the test cases. That was done before Java 5, when annotations didn't exist yet. Nowadays, JUnit 4 (and 5) uses annotations to "flag" test methods.