Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Controlling JUnit test method execution

 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a JUnit test class in which I would like the various "test" methods to be run in a specific order. I have found that the test methods execute in different sequences depending upon the JVM that the JUnit test class is run in.
Looking at the API, I thought that overriding runTest() was the answer but this apparently executes once for each "test" method in the JUnit test class (and there are 8 in my test class). So, it executes the test methods in the proper order but it does so 8 times in my JUnit test class!
My immediate solution is to use a static count variable, check and increment it during runTest() execution. The other possible solution would be to rename all the "test" methods on the JUnit test class. However, it seems there should be an easier way.
I would appreciate any suggestions.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want the tests to run in a specific order? I hope you don't have any state dependencies between test methods...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic