| Author |
TestCase(String) constructor
|
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
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
|
Java API Documentation
The Java Tutorial
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
|
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.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Layne Lund
Ranch Hand
Joined: Dec 06, 2001
Posts: 3061
|
|
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
Joined: Jul 08, 2003
Posts: 24061
|
|
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
Joined: Dec 06, 2001
Posts: 3061
|
|
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
Joined: Dec 06, 2001
Posts: 3061
|
|
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
|
 |
 |
|
|
subject: TestCase(String) constructor
|
|
|