如果你需要測試的控制器有forward,請看看這裏,瞭解如何測試
使用集成web環境進行測試,請使用如下方法創建MockMvc
@Autowired private lateinit var indexViewAction: indexViewAction
private lateinit var mockMvc: MockMvc
@Before
fun before(){
mockMvc = MockMvcBuilders
.standaloneSetup(indexViewAction)
.build()
}
注入你需要的controller,然後將其放入standaloneSetup
這種是使用獨立測試方法進行
首先,我們先準備一個要做單元測試的類,更詳細的在post和get的帶方法測試上,可以直接翻到最後
@RunWith(SpringRunner::class)
/@SpringBootTest(classes = [Application::class]) 需要配外的配置/
@SpringBootTest /* 不需要格外的配置 */
class IndexViewTest {
private lateinit var mockMvc: MockMvc
@Autowired
private lateinit var webApplicationContext: WebApplicationContext
@Before
fun init(){
/*創建一個mock mvc*/
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build()
}
}
想必大家看到了MockMvcBuilders,這個類是用來幫助我們創建一個MockMvc的,name什麼是MockMvc了
// 這個是MockMvc的文檔描述,提供spring mvc的測試支持
/**
- Main entry point for server-side Spring MVC test support.
-
Example
- import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
- import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
- import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
- // …
- WebApplicationContext wac = …;
- MockMvc mockMvc = webAppContextSetup(wac).build();
- mockMvc.perform(get("/form"))
- .andExpect(status().isOk())
- .andExpect(content().mimeType(“text/html”))
- .andExpect(forwardedUrl("/WEB-INF/layouts/main.jsp"));
- @author Rossen Stoyanchev
- @author Rob Winch
- @author Sam Brannen
- @since 3.2
*/
現在我們可以開始測試了,先來簡單的測試一個get post方法
@RunWith(SpringRunner::class)
/@SpringBootTest(classes = [Application::class]) 需要配外的配置/
@SpringBootTest /* 不需要格外的配置 */
class IndexViewTest {
private lateinit var mockMvc: MockMvc
@Autowired
private lateinit var webApplicationContext: WebApplicationContext
@Before
fun init(){
/*創建一個mock mvc*/
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build()
}
/*使用get測試*/
@Test
fun get(){
mockMvc
.get("/index.html") /*指定get方法要使用的url地址*/
.andReturn() /*返回結果*/
.response /*返回內容*/
.contentAsString /*內容作爲html形式*/
.apply(::println) /*打印結果*/
}
/*使用post測試*/
@Test
fun post(){
mockMvc
.post("/index.html") /*指定get方法要使用的url地址*/
.andReturn() /*返回結果*/
.response /*返回內容*/
.contentAsString /*內容作爲html形式*/
.apply(::println) /*打印結果*/
}
}
但是很多時候,我們的測試哪裏有這麼簡單,一般都是帶參數的,那麼帶參數要如何測試呢
重點,這個纔是重點
@RunWith(SpringRunner::class)
/@SpringBootTest(classes = [Application::class]) 需要配外的配置/
@SpringBootTest /* 不需要格外的配置 */
class IndexViewTest {
private lateinit var mockMvc: MockMvc
@Autowired
private lateinit var webApplicationContext: WebApplicationContext
@Before
fun init(){
/*創建一個mock mvc*/
mockMvc = MockMvcBuilders
.webAppContextSetup(webApplicationContext)
.build()
}
/*使用get測試*/
@Test
fun get(){
mockMvc
.get("/index.html") /*指定get方法要使用的url地址*/
.andReturn() /*返回結果*/
.response /*返回內容*/
.contentAsString /*內容作爲html形式*/
.apply(::println) /*打印結果*/
}
/*使用post測試*/
@Test
fun post(){
mockMvc
.post("/index.html") /*指定get方法要使用的url地址*/
.andReturn() /*返回結果*/
.response /*返回內容*/
.contentAsString /*內容作爲html形式*/
.apply(::println) /*打印結果*/
}
/*帶參數的get測試*/
@Test
fun getAndParam(){
mockMvc
.perform(
MockMvcRequestBuilders.get("/index.html")/*設置url地址*/
.param("page","1")/*設置參數*/
.cookie(Cookie("name","lemon"))/*設置cookie*/
.header("username","lemon")/*設置請求頭*/
/*當然還可以設置很多東西,按.就可以看到了*/
)
.andExpect ( /*設置成功條件*/
matchAll( /* 這是一個靜態方法,它的返回值剛好就是andExpect需要的參數*/
status().isOk, /*這是靜態方法,判斷返回類型*/
header().exists("Content-Type"), /*這也是靜態方法,判斷頭裏面是不是有指定的頭,只包含你自己添加的頭,不包括默認頭(也就是哪些不是你設置的頭)*/
content().encoding("utf-8") /*判斷返回的編碼*/
)
)
.andReturn() /*返回結果*/
.response /*返回內容*/
.contentAsString /*內容作爲html形式*/
.apply(::println) /*打印結果*/
}
/*帶參數的post測試*/
@Test
fun postAndParam(){
mockMvc
.perform(
MockMvcRequestBuilders.post("/index.html")/*設置url地址*/
.param("page","1")/*設置參數*/
.cookie(Cookie("name","lemon"))/*設置cookie*/
.header("username","lemon")/*設置請求頭*/
/*當然還可以設置很多東西,按.就可以看到了*/
)
.andExpect ( /*設置成功條件*/
matchAll( /* 這是一個靜態方法,它的返回值剛好就是andExpect需要的參數*/
status().isOk, /*這是靜態方法,判斷返回類型*/
header().exists("Content-Type"), /*這也是靜態方法,判斷頭裏面是不是有指定的頭,只包含你自己添加的頭,不包括默認頭(也就是哪些不是你設置的頭)*/
content().encoding("utf-8") /*判斷返回的編碼*/
)
)
.andReturn() /*返回結果*/
.response /*返回內容*/
.contentAsString /*內容作爲html形式*/
.apply(::println) /*打印結果*/
}
}
注意注意,如果你要是報錯了之後再來看 的話,更加要注意
import org.springframework.test.web.servlet.ResultMatcher.matchAll
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.*
這些東西一定要引入
感謝:更多內容