3

Running controller test classes in order.

I have this test classes below.

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
public class UserControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    public void findAll() throws Exception {
        MvcResult result = mockMvc
                .perform(get("/api/user").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk()).andReturn();

        MockHttpServletResponse response = result.getResponse();
        RestResponse restResponse = mapper.readValue(response.getContentAsString(), RestResponse.class);
        Assert.assertEquals(restResponse.getHttpStatus().name(), HttpStatus.OK.name() );
    }
}

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
public class ProductControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    public void findAll() throws Exception {
        MvcResult result = mockMvc
                .perform(get("/api/product").contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk()).andReturn();

        MockHttpServletResponse response = result.getResponse();
        RestResponse restResponse = mapper.readValue(response.getContentAsString(), RestResponse.class);
        Assert.assertEquals(restResponse.getHttpStatus().name(), HttpStatus.OK.name() );
    }

}

I want to run this controller test classes in order. For example, first UserControllerTest runs after that ProductControllerTest.

How can i do this?

Thank you.

Mehmet Bektaş
  • 173
  • 1
  • 2
  • 15
  • Possible duplicate of [How to run test methods in specific order in JUnit4?](https://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4) – pirho Apr 14 '19 at 08:18

1 Answers1

2

If you have Junit 5 as dependency you can control complete control of the order of the methods but within the test class itself by using @TestMethodOrder.

Regarding the order of the test classes themselves there is not much control available. Maven Failsafe docs say about the <runOrder> configuration:

Supported values are "alphabetical", "reversealphabetical", "random", "hourly" (alphabetical on even hours, reverse alphabetical on odd hours), "failedfirst", "balanced" and "filesystem".

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.0.0-M3</version>
    <configuration>
      <runOrder>alphabetical</runOrder>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>integration-test</goal>         
        </goals>
      </execution>
    </executions>
  </plugin>
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63