测试
- 自动生成的Test类中,@RunWith(SpringRunner.class) 开启Spring集成测试。@SpringBootTest 自动搜索应用启动类,加载应用的上下文。
@RunWith(SpringRunner.class)
@SpringBootTest
public class ReadinglistApplicationTests
- 针对 Controller测试,可以忽略RequestMapping等标签,直接测试方法本身,但是无法处理post请求,也无法测试表单和参数的绑定。spring boot有两种方法可以模拟http请求测试:
- spring mock mvc:在模拟servlet容器里测试控制器。启动快。
- web继承测试:在嵌入式servlet容器中启动应用测试。接近真实环境。
模拟spring mvc
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setupMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
- 编排测试逻辑。需要使用模拟请求测试和校验类MockMvcRequestBuilders,MockMvcResultMatchers,Matchers。
package com.example.readinglist;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.hamcrest.Matchers.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ReadinglistApplicationTests {
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setupMockMvc() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void testBook() throws Exception {
Book book = getBook();
String read1Url = "/readingList/reader1";
mockMvc.perform(get(read1Url))
.andExpect(status().isOk())
.andExpect(view().name("readingList"))
.andExpect(model().attributeExists("books"))
.andExpect(model().attribute("books", is(empty())));
mockMvc.perform(post(read1Url)
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("title", book.getTitle())
.param("author", book.getAuthor())
.param("isbn", book.getIsbn())
.param("description", book.getDescription()))
.andExpect(status().is3xxRedirection())
.andExpect(header().string("Location", read1Url));
mockMvc.perform(get(read1Url))
.andExpect(status().isOk())
.andExpect(view().name("readingList"))
.andExpect(model().attributeExists("books"))
.andExpect(model().attribute("books", hasSize(1)))
.andExpect(model().attribute("books", contains(samePropertyValuesAs(book))));
}
private Book getBook() {
Book book = new Book();
book.setReader("reader1");
book.setId(1L);
book.setTitle("title1");
book.setAuthor("author1");
book.setIsbn("12345");
book.setDescription("desc");
return book;
}
}
服务器测试
- springboot1.5之前可以使用WebIntegrationTest注解,启动嵌入式的servlet容器。参考。
- 同时可以使用Selenium框架进行页面测试。
- spring1.5之后暂未找到替代方案。
部署
- 部署可以直接jar包部署,也可以使用内置的tomcat和外部的tomcat。参考。
jar部署
nohup java -jar house.jar &
exit
内置tomcat
- 在application.yml中微调默认配置,如:
server:
port: 8080
servlet:
context-path: /house
外部tomcat
- 修改启动类,继承SpringBootServletInitializer实现configure方法,相当于之前的web.xml,加载spring上下文。
@SpringBootApplication
@EnableTransactionManagement
public class HouseApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(HouseApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(HouseApplication.class);
}
}
- 由于tomcat有外部容器提供,需要将依赖设为provided,以防与内置容器冲突。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
- 打包为war之后放入tomcat的webapps启动。