SpringBoot-Controller單元測試

在SpringBoot中Controller層的單元測試該怎麼作:java

package com.gy.demo.controller;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * Description: API單元測試
 * 註釋: @AutoConfigureMockMvc 專門進行Controller層的測驗
 * @author geYang
 * @since 2017/12/28
 **/
@SpringBootTest
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
public class AaHelloControllerTest {
    @Autowired
	private MockMvc mockMvc;
	
	@Test
	public void helloTest() throws Exception {
		mockMvc.perform(MockMvcRequestBuilders.get("public/hello"))
				//判斷驗證返回狀態碼是否爲500
				.andExpect(MockMvcResultMatchers.status().isNotFound());
	}

}

    參考大神: https://www.imooc.com/video/14341git

    項目源碼: https://gitee.com/ge.yang/SpringBootweb

相關文章
相關標籤/搜索