如何使用spring集成测试按顺序运行控制器测试类

问题描述 投票:0回答:1

按顺序运行控制器测试类。

我在下面有这个测试类。

@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() );
    }

}

我想按顺序运行这个控制器测试类。例如,第一个UserControllerTest在ProductControllerTest之后运行。

我怎样才能做到这一点?

谢谢。

spring rest spring-boot spring-test mockmvc
1个回答
0
投票

如果你有Junit 5作为依赖,你可以通过使用@TestMethodOrder控制方法顺序的完全控制,但在测试类本身内。

关于测试类本身的顺序,没有太多控制可用。关于<runOrder>配置的Maven Failsafe文档说:

支持的值是“按字母顺序”,“反向字母”,“随机”,“每小时”(偶数小时按字母顺序排列,奇数小时反向按字母顺序排列),“先失败”,“平衡”和“文件系统”。

  <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>
© www.soinside.com 2019 - 2024. All rights reserved.