Spring Boot 1.4测试的简单理解
2017-12-14 19:40
357 查看
首先maven要引入
先看一段代码
1、@RunWith(SpringRunner.class) 告诉JUnit运行使用Spring的测试支持。SpringRunner是SpringJUnit4ClassRunner的新名字,这个名字只是让名字看起来简单些。
2、@SpringBootTest的意思是“带有Spring Boot支持的引导程序”(例如,加载应用程序、属性,为我们提供Spring Boot的所有精华部分)。
3、webEnvironment属性允许为测试配置特定的“网络环境”。你可以利用一个MOCK小服务程序环境开始你的测试,或者使用一个运行在RANDOM_PORT 或者 DEFINED_PORT上的真正的HTTP服务器。
4、如果我们想要加载一个特定的配置,我们可以用@SpringBootTest class属性。在这个实例中,我们省略classes就意味着测试要首次尝试从任意一个inner-classes中加载@ configuration,如果这个尝试失败了,它会在你主要的@SpringBootApplicationclass中进行搜索。
------------------------------------------------------------------------------------------------------------------------------
其中IndexController是你写的controller
这个是第二种测试方法:
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
@AutoConfigureMockMvc
public class IndexControllerTest {
@Autowired
protected MockMvc mvc;
@LocalServerPort // 注入端口
private Integer port;
@Autowired
private TestRestTemplate restTemplate;
private static final Logger logger = LoggerFactory.getLogger(IndexControllerTest.class);
@Test
public void t() throws Exception {
assertNotNull(mvc);
logger.info("# port:" + port.toString());
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/h",
String.class)).contains("hello");
// mvc.perform(MockMvcRequestBuilders.get("/h"))
// .andExpect(MockMvcResultMatchers.status().isOk());
}
}[/code]
是第三种测试方法,直接利用TestRestTemplate类
spring-boot-starter-test这个包。
先看一段代码
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) public class MyTest { @Autowired private TestRestTemplate restTemplate; @Test public void test() { this.restTemplate.getForEntity( "/{username}/vehicle", String.class, "Phil"); } }
1、@RunWith(SpringRunner.class) 告诉JUnit运行使用Spring的测试支持。SpringRunner是SpringJUnit4ClassRunner的新名字,这个名字只是让名字看起来简单些。
2、@SpringBootTest的意思是“带有Spring Boot支持的引导程序”(例如,加载应用程序、属性,为我们提供Spring Boot的所有精华部分)。
3、webEnvironment属性允许为测试配置特定的“网络环境”。你可以利用一个MOCK小服务程序环境开始你的测试,或者使用一个运行在RANDOM_PORT 或者 DEFINED_PORT上的真正的HTTP服务器。
4、如果我们想要加载一个特定的配置,我们可以用@SpringBootTest class属性。在这个实例中,我们省略classes就意味着测试要首次尝试从任意一个inner-classes中加载@ configuration,如果这个尝试失败了,它会在你主要的@SpringBootApplicationclass中进行搜索。
------------------------------------------------------------------------------------------------------------------------------
其中IndexController是你写的controller
@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class IndexTest { private MockMvc mvc; @Autowired private IndexController indexController; @Before public void setup() { this.mvc = MockMvcBuilders.standaloneSetup(indexController).build(); } @Test public void t() throws Exception { assertNotNull(mvc); mvc.perform(MockMvcRequestBuilders.get("/h"). accept(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(MockMvcResultMatchers.status().isOk()); } }
这个是第二种测试方法:
package com.hexun.bdc.auth.controller; import com.hexun.bdc.auth.client.Application; 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.context.embedded.LocalServerPort; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; 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.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertNotNull; /** * Created by iceblue on 2017/3/26. [code]webEnvironment=RANDOM_PORT表示启动服务时端口随机(避免端口冲突)
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
@AutoConfigureMockMvc
public class IndexControllerTest {
@Autowired
protected MockMvc mvc;
@LocalServerPort // 注入端口
private Integer port;
@Autowired
private TestRestTemplate restTemplate;
private static final Logger logger = LoggerFactory.getLogger(IndexControllerTest.class);
@Test
public void t() throws Exception {
assertNotNull(mvc);
logger.info("# port:" + port.toString());
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/h",
String.class)).contains("hello");
// mvc.perform(MockMvcRequestBuilders.get("/h"))
// .andExpect(MockMvcResultMatchers.status().isOk());
}
}[/code]
是第三种测试方法,直接利用TestRestTemplate类
package com.hexun.bdc.auth.controller; import com.hexun.bdc.auth.client.Application; 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.boot.test.web.client.TestRestTemplate; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertNotNull; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes = Application.class) public class IndexTest2 { @Autowired private TestRestTemplate restTemplate; private static final Logger logger = LoggerFactory.getLogger(IndexTest2.class); @Test public void t() throws Exception { assertNotNull(restTemplate); String body = this.restTemplate.getForObject("/h", String.class); System.out.println("body = " + body); logger.info("body = " + body); } }
相关文章推荐
- java 关于Spring中Aop的简单理解以及SpringBoot如何添加Aop实现步骤
- 回顾SpringAOP,简单的测试,加深理解
- 关于springboot和tomcat的服务能力做下简单的测试
- 关于Spring Boot 的一些简单理解和应用
- Spring Boot 1.4测试的改进
- Spring Boot实战之netty-socketio实现简单聊天室(给指定用户推送消息)
- springboot与ebean的简单整合
- Spring boot简单入门学习
- spring boot Batch 的简单学习
- SpringBoot初始教程之测试(八)
- spring事务隔离和传播机制的简单理解
- Spring boot 教程 一 创建简单的 WEB 工程
- springboot + mybatis +junit 测试用例编写
- idea+maven+springboot搭建web简单项目经历小计(RestController)
- Spring WebSocket简单入门测试Demo(网页及时聊天)
- spring boot + Schedule简单定时任务实现
- 最简单的SpringBoot示例之.yml配置文件
- spring-boot+jpa的简单使用
- 对spring-boot+shiro的学习(借助别人家的框架来理解)
- springboot+mybatis日志显示SQL的最简单方法