Spring Boot 2.X 实战教程(17)测试

17.测试

Spring Boot提供了许多实用程序和注释来帮助您测试应用程序。测试支持由两个模块提供:包含spring-boot-test核心项,和支持测试的spring-boot-test-autoconfigure自动配置。

大多数开发人员使用spring-boot-starter-test“Starter”,它导入Spring Boot测试模块以及JUnit,AssertJ,Hamcrest和许多其他有用的库。

17.1测试范围依赖性

在spring-boot-starter-test“入门”(中test scope)包含以下提供的库:

  1. JUnit 4:单元测试Java应用程序的事实标准。
  2. Spring Test和Spring Boot测试:Spring Boot应用程序的实用程序和集成测试支持。
  3. AssertJ:一个流畅的断言库。
  4. Hamcrest:匹配器对象库(也称为约束或谓词)。
  5. Mockito:一个Java 模拟框架。
  6. JSONassert:JSON的断言库。
  7. JsonPath:JSON的XPath。

17.2测试Spring Boot应用程序

Spring Boot应用程序是一个Spring ApplicationContext,所以没有什么特别的东西可以用来测试它超出你通常使用的Spring语境。

Spring Boot提供了一个@SpringBootTest注释,spring-test @ContextConfiguration当您需要Spring Boot功能时,它可以用作标准注释的替代方法。注释的工作原理是通过 创建 ApplicationContext测试中使用的SpringApplication。除了 @SpringBootTest提供许多其他注释之外,还提供了用于 测试应用程序的更具体的切片

默认情况下,@SpringBootTest不会启动服务器。您可以使用该webEnvironment 属性@SpringBootTest进一步优化测试的运行方式:

  1. MOCK(默认):加载Web ApplicationContext并提供模拟Web环境。使用此批注时,不会启动嵌入式服务器。如果类路径上没有Web环境,则此模式将透明地回退到创建常规非Web ApplicationContext。它可以与Web应用程序一起使用 @AutoConfigureMockMvc或@AutoConfigureWebTestClient用于基于模拟的Web应用程序测试。
  2. RANDOM_PORT:加载a WebServerApplicationContext并提供真实的Web环境。嵌入式服务器启动并在随机端口上侦听。
  3. DEFINED_PORT:加载a WebServerApplicationContext并提供真实的Web环境。嵌入式服务器启动并侦听定义的端口(来自您的 application.properties)或默认端口8080。
  4. NONE:ApplicationContext通过使用SpringApplication但不提供 任何 Web环境(模拟或其他)来加载。

17.2.1 Demo应用测试

Demo应用测试,代码如下所示:

 

package com.example.demo;

 

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import org.springframework.test.web.servlet.MockMvc;

 

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

 

/**

 * Demo应用测试

 *

 * @author 大强

 *

 */

@RunWith(SpringRunner.class)

@SpringBootTest

@AutoConfigureMockMvc

public class DemoApplicationTests {

 

@Test

public void contextLoads() {

}

 

@Autowired

private MockMvc mvc;

 

@Test

public void exampleTest() throws Exception {

this.mvc.perform(get("/")).andExpect(status().isOk()).andExpect(content().string("Hello LiSi"));

}

 

}

17.2.2 城市测试

城市测试,代码如下所示:

package com.example.demo;

 

import org.junit.Assert;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import org.springframework.test.web.servlet.MockMvc;

 

import com.example.demo.city.City;

import com.example.demo.city.CityRepository;

import com.example.demo.city.CityService;

 

import java.util.List;

 

/**

 * 城市测试

 *

 * @author 大强

 */

@RunWith(SpringRunner.class)

@SpringBootTest

@AutoConfigureMockMvc

public class CityTests {

 

@Autowired

private CityRepository cityRepository;

 

@Autowired

CityService cityService;

 

/**

 * 新增城市

 */

@Test

public void addCity() {

City city = new City();

city.setName("西安");

city.setState("有效");

city = cityRepository.save(city);

Assert.assertNotNull(city.getId());

}

 

/**

 * 根据名称查询单个城市测试

 */

@Test

public void getCity() {

City city = new City();

List<City> list = cityRepository.findByName("西安");

if (list.size() > 0) {

city = list.get(0);

}

Assert.assertEquals("西安", city.getName());

}

 

/**

 * 查询所有城市列表测试

 */

@Test

public void getAllCitys() {

Iterable<City> iterable = cityService.findAll();

Assert.assertTrue(iterable.iterator().hasNext());

}

 

/**

 * 修改城市测试

 */

@Test

public void updateCity() {

City city = new City();

List<City> list = cityRepository.findByName("西安");

if (list.size() > 0) {

city = list.get(0);

}

city.setState("无效");

city = cityRepository.save(city);

Assert.assertEquals("无效", city.getState());

}

 

/**

 * 删除城市测试

 */

@Test

public void deleteCity() {

City city = new City();

List<City> list = cityRepository.findByName("西安");

if (list.size() > 0) {

city = list.get(0);

}

cityRepository.delete(city);

list = cityRepository.findByName("西安");

if (list.size() > 0) {

city = list.get(0);

}

Assert.assertEquals(0, list.size());

}

 

}

 

如有疑问,请观看视频:https://edu.csdn.net/course/detail/25550

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大强012

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值