SpringBoot接口使用MockMvc进行单元测试

/**
  * Created by Draven on 2017/11/28.
  * Email:[email protected]
  */
@SpringBootTest
@RunWith(classOf[SpringRunner])
@WebAppConfiguration
class OrderControllerTest {
  private var mockMvc: MockMvc = _

  @Autowired
  private var webApplicationContext: WebApplicationContext = _

  @Before
  def init(): Unit = {
    this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build()
  }

  /**
    * 投资记录接口测试
    */
  @Test
  def listOrderTest(): Unit = {
    var res = this.mockMvc.perform(MockMvcRequestBuilders.post("/order/listInvestOrder.api")
      .param("pageNo", "1")
      .param("pageSize", "20")
      .param("startDate", "2017-06-21")
      .param("order", "ASC")
    )
      .andReturn
      .getResponse
      .getContentAsString
    println(res)
  }

  /**
    * 近期结算接口测试
    */
  @Test
  def listRecentlyStatements(): Unit = {
    var res = this.mockMvc.perform(MockMvcRequestBuilders.post("/order/listRecentlyStatements.api")
      .param("pageNo", "1")
      .param("pageSize", "20")
      .param("startDate", "2017-06-21")
      .param("order", "asc"))
      .andReturn
      .getResponse
      .getContentAsString
    println(res)
  }
}

你可能感兴趣的:(Java后台)