代码清单4-1 用SpringJUnit4ClassRunner对Spring应用程序进行集成测试 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration( classes=AddressBookConfiguration.class) //加载应用程序上下文 public class AddressServiceTests { @Autowired private AddressService addressService; //注入地址服务 @Test public void testService() { //测试地址服务 Address address = addressService.findByLastName("Sheman"); assertEquals("P", address.getFirstName()); assertEquals("Sherman", address.getLastName()); assertEquals("42 Wallaby Way", address.getAddressLine1()); assertEquals("Sydney", address.getCity()); assertEquals("New South Wales", address.getState()); assertEquals("2000", address.getPostCode()); } }
解释:html
SpringJUnit4ClassRunner
:是一个JUnit类运行器,会为JUnit测试加载Spring应用程 序上下文,并为测试类自动织入所需的Bean。@ContextConfiguration
替换为Spring Boot的@SpringApplicationConfiguration
, 这样不只会加载应用程序上下文,还会开启日志、加载外部属性(application.properties或application.yml),以及其余Spring Boot特性。 大多数状况下,为Spring Boot应用程序编写测试时应该用@SpringApplicationConfiguration代替@ContextConfiguration。要恰当地测试一个Web应用程序,须要投入一些实际的HTTP请求,确认它能正确地处理 那些请求。Spring Boot开发者有两个可选的方案能实现这类测试:web
要在测试里设置Mock MVC,可使用MockMvcBuilders,该类提供了两个静态方法:spring
两个方法区别:json
前者同单元测试更加接近,你可能只想让它专一于单一控制器的测试,然后者让Spring加载控制 器及其依赖,以便进行完整的集成测试。segmentfault
/* 代码清单4-2 为集成测试控制器建立Mock MVC */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration( classes = ReadingListApplication.class) //开启Web上下文 @WebAppConfiguration public class MockMvcWebTests { @Autowired private WebApplicationContext webContext; //注入WebApplicationContext private MockMvc mockMvc; @Before public void setupMockMvc() { mockMvc = MockMvcBuilders .webAppContextSetup(webContext) //设置MockMvc .build(); } }
/* 向/readingList发起一个GET请求 */ @Test public void homePage() throws Exception { mockMvc.perform(get("/readingList")) .andExpect(status().isOk()) .andExpect(view().name("readingList")) .andExpect(model().attributeExists("books")) .andExpect(model().attribute("books", is(empty()))); }
/* 向/readingList发起一个POST请求 */ @Test public void postBook() throws Exception { mockMvc.perform(post("/readingList") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .param("title", "BOOK TITLE") .param("author", "BOOK AUTHOR") .param("isbn", "1234567890") .param("description", "DESCRIPTION")) .andExpect(status().is3xxRedirection()) .andExpect(header().string("Location", "/readingList"));
/* 验证刚刚的POST请求 */ //配置指望的图书 Book expectedBook = new Book(); expectedBook.setId(1L); expectedBook.setReader("craig"); expectedBook.setTitle("BOOK TITLE"); expectedBook.setAuthor("BOOK AUTHOR"); expectedBook.setIsbn("1234567890"); expectedBook.setDescription("DESCRIPTION"); //执行GET请求 mockMvc.perform(get("/readingList")) .andExpect(status().isOk()) .andExpect(view().name("readingList")) .andExpect(model().attributeExists("books")) .andExpect(model().attribute("books", hasSize(1))) .andExpect(model().attribute("books", contains(samePropertyValuesAs(expectedBook)))); }
使用Spring Security浏览器
添加依赖spring-mvc
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency>
在建立MockMvc实例时运用Spring Security的配置器安全
@Before public void setupMockMvc() { mockMvc = MockMvcBuilders .webAppContextSetup(webContext) .apply(springSecurity()) .build(); }
使用(具体的安全配置取决于你如何配置Spring Security(或者Spring Boot如何自动配置Spring Security)。)服务器
场景代码:并发
1)请求未经身份验证
/* 请求未经身份验证,重定向回登陆界面 */ @Test public void homePage_unauthenticatedUser() throws Exception { mockMvc.perform(get("/")) .andExpect(status().is3xxRedirection()) .andExpect(header().string("Location", "http://localhost/login")); }
2)请求通过身份验证 Spring Security提供了两个注解:
/* 通过身份验证的请求,使用@WithMockUser */ @Test @WithMockUser(username="craig", password="password", roles="READER") public void homePage_authenticatedUser() throws Exception { ... }
/* 通过身份验证的请求,使用@WithUserDetails */ @Test @WithUserDetails("craig") public void homePage_authenticatedUser() throws Exception { Reader expectedReader = new Reader(); expectedReader.setUsername("craig"); expectedReader.setPassword("password"); expectedReader.setFullname("Craig Walls"); mockMvc.perform(get("/")) .andExpect(status().isOk()) .andExpect(view().name("readingList")) .andExpect(model().attribute("reader", samePropertyValuesAs(expectedReader))) .andExpect(model().attribute("books", hasSize(0))) }
此处没有启动Servlet容器来运行这些测试, Spring的Mock MVC取代了实际的Servlet 容器。它比直接调用控制器方法要好,但它并无真的在Web浏 览器里执行应用程序,验证呈现出的视图。
Spring Boot支持用嵌入式Servlet容器来启动应用程序。
Spring Boot 的 @WebIntegrationTest 注解就是这么作的。 在测试类上添加@WebIntegrationTest注解,能够声明你不只但愿Spring Boot为测试建立应用程序上下文,还要启 动一个嵌入式的Servlet容器。一旦应用程序运行在嵌入式容器里,你就能够发起真实的HTTP请 求,断言结果了。
/* 代码清单4-5 在服务器里启动应用程序,以Spring的RestTemplate对应用程序发起HTTP请求 */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration( classes=ReadingListApplication.class) @WebIntegrationTest public class SimpleWebTest { @Test(expected=HttpClientErrorException.class) public void pageNotFound() { try { RestTemplate rest = new RestTemplate(); rest.getForObject( "http://localhost:8080/bogusPage", String.class); fail("Should result in HTTP 404"); } catch (HttpClientErrorException e) { assertEquals(HttpStatus.NOT_FOUND, e.getStatusCode()); throw e; } } }
@WebIntegrationTest(value={"server.port=0"})
或者 @WebIntegrationTest("server.port=0")
或者 @WebIntegrationTest(randomPort=true)
使用端口
@Value("${local.server.port}") private int port;
rest.getForObject(
"http://localhost:{port}/bogusPage", String.class, port);
添加org.seleniumhq.selenium依赖
代码里使用
1> 配置
/* 在Spring Boot里使用Selenium测试的模板 */ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration( classes=ReadingListApplication.class) @WebIntegrationTest(randomPort=true) public class ServerWebTests { private static FirefoxDriver browser; @Value("${local.server.port}") private int port; //配置Firefox驱动 @BeforeClass public static void openBrowser() { browser = new FirefoxDriver(); browser.manage().timeouts() .implicitlyWait(10, TimeUnit.SECONDS); } //关闭浏览器 @AfterClass public static void closeBrowser() { browser.quit(); } }
2> 测试
用Selenium测试阅读列表应用程序
@Test public void addBookToEmptyList() { String baseUrl = "http://localhost:" + port; browser.get(baseUrl); assertEquals("You have no books in your book list", browser.findElementByTagName("div").getText()); //填充并发送表单 browser.findElementByName("title").sendKeys("BOOK TITLE"); browser.findElementByName("author").sendKeys("BOOK AUTHOR"); browser.findElementByName("isbn").sendKeys("1234567890"); browser.findElementByName("description").sendKeys("DESCRIPTION"); browser.findElementByTagName("form").submit(); //判断列表中是否包含新书 WebElement dl = browser.findElementByCssSelector("dt.bookHeadline"); assertEquals("BOOK TITLE by BOOK AUTHOR (ISBN: 1234567890)", dl.getText()); WebElement dt = browser.findElementByCssSelector("dd.bookDescription"); assertEquals("DESCRIPTION", dt.getText()); }
备注:书上版本比较老,下面补充下新版本的测试方法。 例子摘自segmentfault_chenatu的文章 (仅适用spring-boot 1.4版本之后的写法):
直接调用接口函数进行测试:
@RunWith(SpringRunner.class) @SpringBootTest public class ApiTest { @Autowired MessageApi messageApi; ...
测试controller:
@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class ControllerTest { @Autowired private MockMvc mockMvc; @Test public void testControllerMethods() { MvcResult result = mockMvc.perform(get("/get-receive-message-abstracts").param("siteId", "webtrn").param("uid", "lucy") .param("limit", "100")).andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(10))).andExpect(jsonPath("$[9].title", is("hello0"))).andReturn(); }
mockito使用参考: