先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新软件测试全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
如果你需要这些资料,可以添加V获取:vip1024b (备注软件测试)
正文
========================================================================
Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的集成测试。 在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启和使用它。
org.springframework.boot
spring-boot-starter-test
test
当你的集成测试代码需要用到 Spring Boot 功能时,你可以使用@SpringBootTest注解。
该注解是普通的 Spring 项目(非 Spring Boot 项目)中编写集成测试代码所使用的@ContextConfiguration注解的替代品。其作用是用于确定如何装载 Spring 应用程序的上下文资源
@SpringBootTest
class MysqlDataTestApplicationTests {
@Autowired
TOrderInfoService tOrderInfoService;
@Test
public void tOrderInfoServiceSelect() {
TOrderInfo tOrderInfo = tOrderInfoService.selectByPrimaryKey(1L);
Assertions.assertThat(tOrderInfo).isNotNull();
}
}
当运行 Spring Boot 应用程序测试时,它会自动的从当前测试类所在的包起一层一层向上搜索,直到找到一个@SpringBootApplication或@SpringBootConfiguration注释类为止。以此来确定如何装载 Spring 应用程序的上下文资源。只要你以合理的方式组织你的代码,你项目的主配置通常是可以被发现的。
如果搜索算法搜索不到你项目的主配置文件,将报出异常:
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test
解决办法是,按 Spring Boot 的约定重新组织你的代码结构,或者手工指定你要装载的主配置文件:
@SpringBootTest(classes = {MysqlDataTestApplication.class})
class MysqlDataTestApplicationTests {
@Autowired
TOrderInfoService tOrderInfoService;
@Test
public void tOrderInfoServiceSelect() {
TOrderInfo tOrderInfo = tOrderInfoService.selectByPrimaryKey(1L);
Assertions.assertThat(tOrderInfo).isNotNull();
}
}
2.Spring Boot Web 测试- 启动tomcat
当你想启动一个完整的 HTTP 服务器对 Spring Boot 的 Web 应用编写测试代码时,可以使用@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)注解开启一个随机的可用端口。Spring Boot 针对 REST 调用的测试提供了一个 TestRestTemplate 模板,它可以解析链接服务器的相对地址。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class MysqlDataTestApplicationTests2 {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void test() throws Exception {
String response = restTemplate.getForObject(“/query”, String.class);
Assertions.assertThat(response).contains(“QRC123456789012”);
}
}
3.Spring Boot Web 测试- 不启动tomcat(模拟环境)
@SpringBootTest
@AutoConfigureMockMvc
class MysqlDataTestApplicationTests2 {
@Autowired
private MockMvc mockMvc;
@Test
public void test() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get(“/query”)).andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk());
}
}
========================================================================
上文中描述的两种集成测试的方案,相同的一点是都会构建整个Spring Context。这表示所有声明的bean,而不管声明的方式为何,都会被构建实例,并且都能被依赖。这里隐含的意思是从上到下整条依赖链上的代码都已实现。
- Mock技术
在开发的过程中进行测试,无法满足上述的条件,Mock技术可以让我们屏蔽掉下层的依赖,从而专注于当前的测试目标。Mock技术的思想是,当测试目标的下层依赖的行为是可预期的,那么测试目标本身的行为也是可预期的,测试就是把实际的结果和测试目标的预期结果做比较,而Mock就是预先设定下层依赖的行为表现。
- Mock的流程
1、将测试目标的依赖对象进行mock,设定其预期的行为表现。
2、 对测试目标进行测试。
3、检测测试结果,检查在依赖对象的预期行为下,测试目标的结果是否符合预期。
- Mock的使用场景
1、多人协作时,可以通过mock进行无等待的测试先行。
2、当测试目标的依赖对象需要访问外部的服务,而外部服务不易获得时,可以通过mock来模拟服务可用。
3、当在排查不容易复现的问题场景时,通过mock来模拟问题。
当你想对 Spring MVC 控制器编写单元测试代码时,可以使用@WebMvcTest注解。它提供了自配置的 MockMvc,可以不需要完整启动 HTTP 服务器就可以快速测试 MVC 控制器。
@WebMvcTest(TestController.class)
class TestControllerTests {
@Autowired
private MockMvc mockMvc;
@MockBean
private TOrderInfoService tOrderInfoService;
@Test
public void query() throws Exception {
TOrderInfo tOrderInfo = new TOrderInfo();
tOrderInfo.setId(1L);
tOrderInfo.setMerId(“QRC123456789012”);
Mockito.when(tOrderInfoService.selectByPrimaryKey(1L)).thenReturn(tOrderInfo);
mockMvc.perform(MockMvcRequestBuilders.get(“/query”)).andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.content().string(Matchers.containsString(“QRC123456789012”)));
}
}
使用@WebMvcTest注解时,只有一部分的 Bean 能够被扫描得到,它们分别是:
-
@Controller
-
@ControllerAdvice
-
@JsonComponent
-
Filter
-
WebMvcConfigurer
-
HandlerMethodArgumentResolver
其他常规的@Component(包括@Service、@Repository等)Bean 则不会被加载到 Spring 测试环境上下文中。
如果测试的 MVC 控制器中需要@ComponentBean 的参与,你可以使用@MockBean注解来协助完成
如果使用了mybatis的@MapperScan注解,则mapper也会自动装配,但是却么有DataSource,测试会失败,可以先注释掉@MapperScan注解
@MybatisTest
// 连接真实的数据库,否则连接一个内存中的库,测试会失败
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class TOrderInfoMapperTests {
@Autowired
TOrderInfoMapper tOrderInfoMapper;
@Test
// 测试数据不回滚
@Rollback(false)
// 事务是只读的,如果有更新操作,方法将失败
// @Transactional(readOnly = true)
public void test3() throws Exception {
TOrderInfo orderInfo = new TOrderInfo();
orderInfo.setMerId(“133333333”);
orderInfo.setId(2L);
int i = tOrderInfoMapper.updateByPrimaryKeySelective(orderInfo);
Assertions.assertThat(i).isEqualTo(1);
}
}
service层并不作为一种特殊的层,所以没有什么注解能表示“只构建service层的bean”这种概念。
这里将介绍另一种通用的测试场景,我要测试的是一个普通的bean,没有什么特殊的角色,比如不是担当特殊处理的controller,也不是负责持久化的dao组件,我们要测试的只是一个普通的bean。
上文中我们使用@SpringBootTest的默认机制,它去查找@SpringBootApplication的配置,据此构建Spring的上下文。查看@SpringBootTest的doc,其中有一句是:
Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.
这表示我们可以通过classes属性来指定Configuration类,或者定义内嵌的Configuration类来改变默认的配置。
@SpringBootTest
public class TOrderInfoServiceTest {
@Configuration
static class TOrderInfoServiceConfig {
@Bean
public TOrderInfoService cityService() {
return new TOrderInfoService();
}
}
@Autowired
private TOrderInfoService tOrderInfoService;
@MockBean
private TOrderInfoMapper tOrderInfoMapper;
@Test
public void test() {
TOrderInfo tOrderInfo = new TOrderInfo();
tOrderInfo.setId(1L);
tOrderInfo.setMerId(“QRC123456789012”);
Mockito.when(tOrderInfoMapper.selectByPrimaryKey(1L)).thenReturn(tOrderInfo);
TOrderInfo tOrderInfo1 = tOrderInfoService.selectByPrimaryKey(1L);
Assertions.assertThat(tOrderInfo1).isNotNull();
}
}
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
nfo.setMerId(“QRC123456789012”);
Mockito.when(tOrderInfoMapper.selectByPrimaryKey(1L)).thenReturn(tOrderInfo);
TOrderInfo tOrderInfo1 = tOrderInfoService.selectByPrimaryKey(1L);
Assertions.assertThat(tOrderInfo1).isNotNull();
}
}
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注软件测试)
[外链图片转存中…(img-7k1GwEPf-1713416060511)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!