• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Question on addTest method of Junit

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all, I got a question on the addTest method of TestSuite class. I wrote a TestSuite class with the static suite method like this:
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTestSuite(CalTest.class);
return suite;
}
where CalTest is a class inheriting from TestCase class. This part of code is ok. However if I change to this
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new CalTest()); --- difference
return suite;
}
The launch of the testrunner fails and reports a AssertionFailedError. On the JUnit api doc, the addTest(Test test) method does not have any explanation. I would like to know what is wrong with the second method.
 
author & internet detective
Posts: 40036
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default constructor doesn't tell Junit which test case to add to the suite. You need to call the constructor that takes a string. For example,


However, this only runs one test. If you want to run all the tests, as I suspect, you can add the whole test suite. There are two ways of doing this (one you already had)
 
Boyan Wu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Jeanne. You solved my puzzle. I used to think the fName private field in the TestCase class indicates the name of the test class, a usual way in designing class though, it turns out this is the method name I want to run.
 
    Bookmark Topic Watch Topic
  • New Topic