0

I am using Junit for unit testing in my project. I need to make ordering junit test classes execution or make one of them run last when i run the package containing all test classes or when those are running when doing a maven install. thank you for your help.

saadoune
  • 317
  • 4
  • 18
  • 2
    http://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4 – Jose Luis Apr 07 '16 at 10:41
  • 6
    This sounds a bit fishy. You shouldn't be writing unit tests that are dependent on each other. They should be distinct tests that could be run separately, in which case the order they run is insignificant. If your test has prerequisites then use the setup method. – Ben Thurley Apr 07 '16 at 10:42
  • In addition to what @BenThurley said above, it might better if you explain /why/ you want to run a specific test last and we can help you with that problem – tddmonkey Apr 07 '16 at 10:43
  • In fact it is bad to make unit tests dependents on each others, but i prefer to execute slower test at end, the most important point is to get fails as quick as possible – Lho Ben Oct 19 '18 at 21:57

2 Answers2

1

Unit test has to be Unit. If you are trying to make some sort of unit tests which are dependent on each other then its a bad idea.

Although you can use @BeforeClass or @AfterClass Annotation for setting up your configuration for test or cleanup code respectively.

Muhammad Suleman
  • 2,718
  • 2
  • 23
  • 31
1

By design, JUnit does not specify the execution order of test method invocations. Until now, the methods were simply invoked in the order returned by the reflection API. However, using the JVM order is unwise since the Java platform does not specify any particular order, and in fact JDK 7 returns a more or less random order. Of course, well-written test code would not assume any order, but some does, and a predictable failure is better than a random failure on certain platforms.

JUnit 4.11 will by default use a deterministic, but not predictable, order (MethodSorters.DEFAULT). To change the test execution order simply annotate your test class using @FixMethodOrder and specify one of the available MethodSorters:

  1. @FixMethodOrder(MethodSorters.JVM): Leaves the test methods in the order returned by the JVM. This order may vary from run to run.

  2. @FixMethodOrder(MethodSorters.NAME_ASCENDING): Sorts the test methods by method name, in lexicographic order.

Anto W
  • 41
  • 8