0

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.

user_27
  • 201
  • 7
  • 19

1 Answers1

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