• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Question on addTest method of Junit

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic