1. 程式人生 > >SpringBoot介面使用MockMvc進行單元測試

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)
  }
}