I am building spring boot application with unit testing. I have entities those are dependent on each other, Eg: I have User and Roles. To create User i should need Roles. I am testing roles and user object resources with MockMvc. I created Test class for Each Entity. When i run test UserTest class is executing Before Role Test class. So, all my tests are failing. I need help to run Test classes in an order.
Asked
Active
Viewed 446 times
0
-
What testing framework are you using? JUnit 4, JUnit Jupiter, TestNG, ...? – Sam Brannen Jul 07 '19 at 13:20
-
In any case, true unit tests should not be dependent on other tests. So the best course of action is to ensure that you have the proper test fixture setup in place for each unit test. – Sam Brannen Jul 07 '19 at 13:20
-
@SamBrannen, I am using JUnit 4. – user_27 Jul 08 '19 at 16:36
1 Answers
0
As I mentioned in the comments, the best solution to such a problem is to avoid dependencies between test classes.
This can be achieved via proper test fixture setup and tear down (e.g., @Before
and @After
methods in JUnit 4).
Having said that, however, it is possible to order test classes in JUnit 4 if you execute them via a suite as in the following example.
@RunWith(Suite.class)
@SuiteClasses({RoleTests.class, UserTests.class})
public class MyTestSuite {
public static class RoleTests {
@Test
public void roleTest() {
System.err.println("roleTest");
}
}
public static class UserTests {
@Test
public void userTest() {
System.err.println("userTest");
}
}
}
That always prints:
roleTest
userTest

Sam Brannen
- 29,611
- 5
- 104
- 136