Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Andrey Frolov
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34401
346
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic