wood burning stoves*
The moose likes Testing and the fly likes JUnit. How to pass parameters into Test classes from suite() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "JUnit. How to pass parameters into Test classes from suite() ?" Watch "JUnit. How to pass parameters into Test classes from suite() ?" New topic
Author

JUnit. How to pass parameters into Test classes from suite() ?

Andrey Frolov
Greenhorn

Joined: Apr 16, 2011
Posts: 1
Hi everybody.

What do I have now (example code, just fore more clear explanation).

Class which is used for running all tests:



Test classes (FirstTest, SecondTest) uses field "id", which is declared in their common super class somehow like this:


So, what do I need.
Want to write class AllMultiIdsTests, which also will be responsible for running test classes FirstTest and SecondTest.
But unlike AllTests, it would be do it for ids collection.

Something like this:


Or it's also possible to rewrite FirstTest, SecondTest in a way when they will use single id or ids collection in depend on some new field. But I also need to pass value of this field from AllMultiIdsTests and AllTests... How can I do it?

For now I haven't found anything related to configuring Test classes from class which run them all...

PS: Sorry for any errors in English if there are some. Thanks.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30502
    
150

Andrey,
Welcome to CodeRanch! This is an interesting question. I can think of two approaches.

Option 1
If you add the same test multiple times, JUnit will run it. This means you can create a dummy test where the the test itself sets the property.
Pros:
1) The suite is still a JUnit test.
2) You get one set of output
Cons:
1) You are relying on JUnit continuing to run the tests in the same order as specified in the test suite. It is documented that the order isn't guaranteed so you would be introducing code that relies on a side effect.
2) It isn't apparent which test has failed. (You can mitigate this by having DummyTestToSetProperty output what the current property is.)



Option 2
Run the test suite itself multiple times from a main method.
Pros:
1) Cleaner and not subject to a side effect
Cons:
1) Runs multiple suites with separate output. Could be a problem for your build tool


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JUnit. How to pass parameters into Test classes from suite() ?