package com.springboot.learn.test;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.springboot.learn.LearnMavenApplication;
import com.springboot.learn.reading.entity.Book;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = LearnMavenApplication.class)
@WebAppConfiguration
public class MockMvcWebTests {
@Autowired
private WebApplicationContext webContext;
private MockMvc mockMvc;
@Before
public void setupMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webContext).build();
}
@Test
public void homePage() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/reading/list/tom"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("views/reading/reading-list"))
.andExpect(MockMvcResultMatchers.model().attributeExists("reader"))
.andExpect(MockMvcResultMatchers.model().attribute("books", Matchers.is(Matchers.empty())));
}
@Test
public void postBook() throws Exception {
mockMvc
.perform(MockMvcRequestBuilders.post("/reading/save/tom")
.contentType(MediaType.APPLICATION_FORM_URLENCODED) // application/x-www-form-urlencoded
.param("title", "BOOK TITLE")
.param("author", "BOOK AUTHOR")
.param("isbn", "1234567890")
.param("description", "DESCRIPTION"))
.andExpect(MockMvcResultMatchers.status().is3xxRedirection())
.andExpect(MockMvcResultMatchers.header().string("Location", "/reading/list/tom"));
Book expectedBook = new Book();
expectedBook.setId(1L);
expectedBook.setReader("craig");
expectedBook.setTitle("BOOK TITLE");
expectedBook.setAuthor("BOOK AUTHOR");
expectedBook.setIsbn("1234567890");
expectedBook.setDescription("DESCRIPTION");
mockMvc
.perform(MockMvcRequestBuilders.get("/reading/list/tom"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.view().name("views/reading/reading-list"))
.andExpect(MockMvcResultMatchers.model().attributeExists("reader"))
.andExpect(MockMvcResultMatchers.model().attribute("books", Matchers.hasSize(1)));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论








收起资源包目录



































































































共 48 条
- 1
资源评论

- phochahasau2019-10-01真心贵了点m187381020142019-10-12不好意思,我改改

m18738102014
- 粉丝: 3
- 资源: 1
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
