SpringBoot入门(22)--springBoot测试

本文详细介绍了Spring Boot应用的测试方法,包括环境配置、Mapper测试、Controller测试等关键环节,并对比了不同测试策略的特点和适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、添加测试依赖

Spring-boot-starter-test

Scope:test

 

2、添加测试注解

@Runwith(SrpingRunner.class)

@SpringBootTest

 

3、在测试环境单独装配bean

使用@SpringBootTest(classes=xx.class)加载

加上测试配置类需注解@TestConfiguration

而使用@Configuration是无效的

 

测试1:环境Evn的测试

直接使用Enviroment接口可以自动装配,获取属性值

优先取测试环境的配置文件

 

2、给配置加配置项适用于测试类

方法一:使用注解@SpringBootTest的属性properties=”app.version=1”

方法二:使用@before接口注解,在接口中添加配置项

 

测试2:Mapper的测试

使用注解@MockBean注入Mapper实例,使用BDDMockito这个类模拟

抛异常预测:

  1. 控制器Controller测试

依赖类TestRestTemplate工具测试控制器

@SpringBooTest加载Spring整个容器

测试一个带参数的Controller

Get方式的参数

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class BookRestTest {
	
	@Autowired
	private TestRestTemplate testRest;

	@Test
	public void testHome() {
		String body = testRest.getForObject("/book/home", String.class);
		Assert.assertEquals("book home", body);
	}
	
	@Test
	public void testShow() {
		String body = testRest.getForObject("/book/show?id=100", String.class);
		
		Assert.assertEquals("book100", body);
	}

}

 

 

2、控制器Controller测试,方式二,一下方法不会加载Spring容器

使用测试类注解@WebMvcTest(controllers=xxController.class) 通过属性指定控制器

使用MockMvc接口模拟测试Controller

@RunWith(SpringRunner.class)
@WebMvcTest(controllers=BookRest.class)
public class BookRestTest2 {
	
	
	@Autowired
	private MockMvc mvc;

	@Test
	public void testHome() throws Exception {
//		String body = testRest.getForObject("/book/home", String.class);
//		Assert.assertEquals("book home", body);
		
		mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk());
	}
	
	@Test
	public void testShow() throws Exception {
		mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id", "100")).andExpect(MockMvcResultMatchers.status().isOk());
		
	}

}

总结:

多个参数测试:

该种方法不会加载Spring整个容器,只会加载测试对象;

@SpringBooTest加载Spring整个容器

3、两者结合使用:使用注解@SpringBootTest加载整个容器bean,加上@AutoConfigureMockMvc注解开启MockMvc对象

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.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class BookRestTest3 {
	
	@Autowired
	private TestRestTemplate testRest;
	
	@Autowired
	private MockMvc mvc;

	@Test
	public void testHome() throws Exception {
		String body = testRest.getForObject("/book/home", String.class);
		Assert.assertEquals("book home", body);
		
//		mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk());
	}
	
	@Test
	public void testShow() throws Exception {
		mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id", "100")).andExpect(MockMvcResultMatchers.status().isOk());
		
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值