How to make a Junit test class run last when running test package

时间:2016-04-07 10:38:30

标签: java unit-testing junit4

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.

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分: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.

相关问题