File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TestCase(String) constructor

 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the String parameter to this constructor used for? The JUnit javadocs don't give much information:

Constructs a test case with the given name.

Is this name for output purposes only? Or is it used in the Reflection mechanism that determines which method to run?

Layne
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the name of the test method the TestCase will run; so you can use it to build your own TestSuite if the reflective builder won't find the right things.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I use the noargs constructor instead, will it just run all the testXxx() methods automagically? I know that JUnit uses reflection to do a lot of its work; I just don't know all the details, yet.

Layne
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, a TestCase won't do that reflection, but a TestSuite will. There's more than one way to do it; it will use that String-argument constructor to build the right test cases.

Look at the "Test infected" article here. It shows two different ways of building TestSuite objects:



In the second example, the TestSuite constructor uses reflection to do exactly what the first one does "manually". But if some of your test methods aren't named "testXXX" for example, or if some of your test cases need constructor arguments, then you can mix the two styles:

 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read Test Infected when I first started to learn about JUnit. Sounds like I need to re-read it now that I understand a little more about what is going on. I've been using the "TestSuite suite = new TestSuite(MoneyTest.class);" idiom almost exclusively in all my testing. Now I'm in a situation where I added a constructor to my test class to create test cases based on some parameters (see the discussion here). I wasn't sure how to get it to call my testXxx methods. Looks like I need to create a new objet to run each method then. That's all right because, at least for now, there are no other test methods.

Layne
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, I reread some parts of the Test Infected article and it definitely makes more sense after I've already put many of the techniques into practice. It looks like there is another way to run the test methods: overriding the runTest() method. I might use this since I can then use a more descriptive name for the test.

Thanks for your help, EFH.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic