前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >一起玩转微服务(14)——单元测试

一起玩转微服务(14)——单元测试

作者头像
cloudskyme
发布于 2020-06-30 02:38:42
发布于 2020-06-30 02:38:42
77600
代码可运行
举报
文章被收录于专栏:cloudskymecloudskyme
运行总次数:0
代码可运行

作为一名java开发者,相信你或多或少的接触过单元测试,对于测试来讲它是一门能够区分专业开发人员与业余开发人员的重要学科,这篇文章将对java中最常见的一个单元测试框架junit进行一个梳理和讲解。

为什么需要单元测试

在平时的开发当中,一个项目往往包含了大量的方法,可能有成千上万个。如何去保证这些方法产生的结果是我们想要的呢?当然了,最容易想到的一个方式,就是我们通过System.out来输出我们的结果,看看是不是满足我们的需求,但是项目中这些成千上万个方法,我们总不能在每一个方法中都去输出一遍嘛。这也太枯燥了。这时候用我们的单元测试框架junit就可以很好地解决这个问题。

junit如何解决这个问题的呢?答案在于内部提供了一个断言机制,他能够将我们预期的结果和实际的结果进行比对,判断出是否满足我们的期望。

预备工作

junit4是一个单元测试框架,既然是框架,这也就意味着jdk并没有为我们提供api,因此在这里我们就需要导入相关的依赖。

junit4是一个单元测试框架,既然是框架,这也就意味着jdk并没有为我们提供api,因此在这里我们就需要导入相关的依赖。

这里的版本是4.12。当然还有最新的版本。你可以手动选择。这里选用的是4的版本。

案例

这里我们要测试的功能超级简单,就是加减乘除法的验证。

然后我们看看如何使用junit去测试。

以上就是我们的单元测试,需要遵循一下规则:

  • •每一个测试方法上使用@Test进行修饰
  • •每一个测试方法必须使用public void 进行修饰
  • •每一个测试方法不能携带参数
  • •测试代码和源代码在两个不同的项目路径下
  • •测试类的包应该和被测试类保持一致
  • •测试单元中的每个方法必须可以独立测试

以上的6条规则,是在使用单元测试的必须项,当然junit也建议我们在每一个测试方法名加上test前缀,表明这是一个测试方法。

assertEquals是一个断言的规则,里面有两个参数,第一个参数表明我们预期的值,第二个参数表示实际运行的值。

我们运行一下测试类,就会运行每一个测试方法,我们也可以运行某一个,只需要在相应的测试方法上面右键运行即可。如果运行成功编辑器的控制台不会出现错误信息,如果有就会出现failure等信息。

运行流程

在上面的每一个测试方法中,代码是相当简单的,就一句话。现在我们分析一下这个测试的流程是什么:

在上面的代码中,我们使用了两个测试方法,还有junit运行整个流程方法。我们可以运行一下,就会出现下面的运行结果:

从上面的结果我们来画一张流程图就知道了:

如果我们使用过SSM等其他的一些框架,经常会在before中添加打开数据库等预处理的代码,也会在after中添加关闭流等相关代码。

注解

对于@Test,里面有很多参数供我们去选择。我们来认识一下

  • •@Test(expected=XX.class) 这个参数表示我们期望会出现什么异常,比如说在除法中,我们1/0会出现ArithmeticException异常,那这里@Test(expected=ArithmeticException.class)。在测试这个除法时候依然能够通过。
  • •@Test(timeout=毫秒 ) 这个参数表示如果测试方法在指定的timeout内没有完成,就会强制停止。
  • •@Ignore 这个注解其实基本上不用,他的意思是所修饰的测试方法会被测试运行器忽略。•@RunWith 更改测试运行器。

测试套件

如果我们的项目中如果有成千上万个方法,那此时也要有成千上万个测试方法嘛?如果这样junit使用起来还不如System.out呢,现在我们认识一下测试嵌套的方法,他的作用是我们把测试类封装起来,也就是把测试类嵌套起来,只需要运行测试套件,就能运行所有的测试类了。

下面我们使用测试套件,把这些测试类嵌套在一起。

参数化设置

什么是参数化设置呢?在一开始的代码中我们看到,测试加法的时候是1+1,不过我们如果要测试多组数据怎么办?总不能一个一个输入,然后运行测试吧。这时候我们可以把我们需要测试的数据先配置好。

这时候再去测试,只需要去选择相应的值即可,避免了我们一个一个手动输入。

spring boot + junit

通过spring suite tools新建工程

1. Controller

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@RestController
@RequestMapping
public class BookController {
    @RequestMapping("/books")
    public String book() {
        System.out.println("controller");
        return "book";
    }
}

Test1 引入Spring上下文,但不启动tomcat

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@RunWith(SpringRunner.class)
@SpringBootTest  //引入Spring上下文 -> 上下文中的 bean 可用,自动注入
public class BookControllerTest {
    
    @Autowired
    private BookController bookController;  //自动注入
    
    @Test
    public void testControllerExists() {
        Assert.assertNotNull(bookController);
    }
    
}

Test2 引入Spring上下文,且启动Tomcat 模拟生产环境,接收Http请求

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.cloud.skyme;

import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
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.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @author zhangfeng
 * web单元测试
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class Chapter0302junitApplicationTests {
	
	@LocalServerPort
    private int port;
	
	@Autowired
	private TestRestTemplate restTemplate;
    
    @Test
    public void testControllerExists() {
    	Assert.assertEquals(this.restTemplate.getForObject("http://localhost:" + port + "/books", String.class), "book");
    }

}

@RunWith(SpringRunner.class),让测试运行于Spring测试环境,此注释在org.springframework.test.annotation包中提供。 @SpringBootTest指定Sspring Bboot程序的测试引导入口。 TestRestTemplate是用于测试rest接口的模板类。 运行单元测试,测试上面边构建的Wweb地址,可以看到输出的测试结果与期望的结果相同.

运行单元测试,得到与期望相同的结果。

javascript    44行

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
13:31:03.722 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
13:31:03.739 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
13:31:03.801 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.cloud.skyme.Chapter0302junitApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
13:31:03.830 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.cloud.skyme.Chapter0302junitApplicationTests], using SpringBootContextLoader
13:31:03.837 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.cloud.skyme.Chapter0302junitApplicationTests]: class path resource [com/cloud/skyme/Chapter0302junitApplicationTests-context.xml] does not exist
13:31:03.838 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.cloud.skyme.Chapter0302junitApplicationTests]: class path resource [com/cloud/skyme/Chapter0302junitApplicationTestsContext.groovy] does not exist
13:31:03.838 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.cloud.skyme.Chapter0302junitApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
13:31:03.839 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.cloud.skyme.Chapter0302junitApplicationTests]: Chapter0302junitApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
13:31:03.918 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.cloud.skyme.Chapter0302junitApplicationTests]
13:31:04.070 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [C:\java\workspace\microservice\chapter0302junit\target\classes\com\cloud\skyme\Chapter0302junitApplication.class]
13:31:04.073 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.cloud.skyme.Chapter0302junitApplication for test class com.cloud.skyme.Chapter0302junitApplicationTests
13:31:04.225 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.cloud.skyme.Chapter0302junitApplicationTests]: using defaults.
13:31:04.226 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
13:31:04.243 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
13:31:04.244 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
13:31:04.244 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@7133da86, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3232a28a, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@73e22a3d, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@47faa49c, org.springframework.test.context.support.DirtiesContextTestExecutionListener@28f2a10f, org.springframework.test.context.event.EventPublishingTestExecutionListener@f736069, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@6da21078, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@7fee8714, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@4229bb3f, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@56cdfb3b, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@2b91004a, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@20ccf40b]
13:31:04.250 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@6cd28fa7 testClass = Chapter0302junitApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@614ca7df testClass = Chapter0302junitApplicationTests, locations = '{}', classes = '{class com.cloud.skyme.Chapter0302junitApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@3b07a0d6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@14d3bc22, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@45b9a632, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@5e316c74, org.springframework.boot.test.context.SpringBootTestArgs@1], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> false]], class annotated with @DirtiesContext [false] with mode [null].
13:31:04.267 [main] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[DefaultTestContext@6cd28fa7 testClass = Chapter0302junitApplicationTests, testInstance = com.cloud.skyme.Chapter0302junitApplicationTests@31fa1761, testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@614ca7df testClass = Chapter0302junitApplicationTests, locations = '{}', classes = '{class com.cloud.skyme.Chapter0302junitApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@3b07a0d6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@14d3bc22, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@45b9a632, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@5e316c74, org.springframework.boot.test.context.SpringBootTestArgs@1], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> false]]].
13:31:04.306 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=0}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2020-06-28 13:31:04.940  INFO 8376 --- [           main] c.c.s.Chapter0302junitApplicationTests   : Starting Chapter0302junitApplicationTests on WIN-55FHBQI56BD with PID 8376 (started by Administrator in C:\java\workspace\microservice\chapter0302junit)
2020-06-28 13:31:04.942  INFO 8376 --- [           main] c.c.s.Chapter0302junitApplicationTests   : No active profile set, falling back to default profiles: default
2020-06-28 13:31:09.134  INFO 8376 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 0 (http)
2020-06-28 13:31:09.160  INFO 8376 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-06-28 13:31:09.161  INFO 8376 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-06-28 13:31:09.372  INFO 8376 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-06-28 13:31:09.372  INFO 8376 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4316 ms
2020-06-28 13:31:10.029  INFO 8376 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-06-28 13:31:10.655  INFO 8376 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 59724 (http) with context path ''
2020-06-28 13:31:10.673  INFO 8376 --- [           main] c.c.s.Chapter0302junitApplicationTests   : Started Chapter0302junitApplicationTests in 6.362 seconds (JVM running for 8.218)
2020-06-28 13:31:11.423  INFO 8376 --- [o-auto-1-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-06-28 13:31:11.423  INFO 8376 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-06-28 13:31:11.461  INFO 8376 --- [o-auto-1-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 37 ms
controller
2020-06-28 13:31:13.497  INFO 8376 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

这样,一个web应用从构建到单元测试就都已经完成了,可见,构建一个Spring Web MVC的应用就是如此简单。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-06-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
2021年 最新 多阶段构建dockerfile实现java源码编译打jar包并做成镜像
多阶段构建指在Dockerfile中使用多个FROM语句,每个FROM指令都可以使用不同的基础镜像,并且是一个独立的子构建阶段。使用多阶段构建打包Java应用具有构建安全、构建速度快、镜像文件体积小等优点.
猫头虎
2024/04/07
5240
2021年 最新 多阶段构建dockerfile实现java源码编译打jar包并做成镜像
单元测试(Spring)
YGingko
2017/12/28
4.8K0
第三十五章:SpringBoot与单元测试的小秘密
单元测试对于开发人员来说是非常熟悉的,我们每天的工作也都是围绕着开发与测试进行的,在最早的时候测试都是采用工具Debug模式进行调试程序,后来Junit的诞生也让程序测试发生了很大的变化。我们今天来讲解下基于SpringBoot结合Junit怎么来完成单元测试。 本章目的 基于SpringBoot平台整合Junit分别完成客户端、服务端的单元测试。 SpringBoot 企业级核心技术学习专题 专题 专题名称 专题描述 001 Spring Boot 核心技术 讲解SpringBoot一些企业级层面的核心组
恒宇少年
2018/06/27
1.4K0
quartz
Quartz是功能强大的开源作业调度库,几乎可以集成到任何Java应用程序中-从最小的独立应用程序到最大的电子商务系统。Quartz可用于创建简单或复杂的计划,以执行数以万计,数以万计的工作。任务定义为标准Java组件的作业,它们实际上可以执行您可以对其执行的任何编程操作。Quartz Scheduler包含许多企业级功能,例如对JTA事务和集群的支持。
阿超
2022/08/16
1.4K0
quartz
SpringBoot单元测试(实例)
这里我们分别使用@WebMvcTest和@SpringBootTest两种方式测试一个控制器方法是否满足测试用例。
别团等shy哥发育
2023/02/25
1.3K0
SpringBoot单元测试(实例)
基于SpringBoot聊单元测试的分层
之前分享了关于质量内建的话题关于单元测试引起了大家的讨论,对于单元测试这件事情本身是比较熟悉的,但大家的反馈是比较难执行,矛盾在于很多测试做不了单元测试,或者让测试做性价比不是很高,这件事情推给开发之后又容易不了了之,其中一个很重要的点是,测试和开发没有同频对话的能力,各种细节难以敲定,落地的实际价值不容易度量,所以这篇文章我就基于常见的springboot框架,聊一聊单元测试分层的几种实践方式,从测试的视角给同学们一些知识面的拓展,也让大家熟悉下单元测试的常见玩法。
周辰晨
2022/09/20
8410
基于SpringBoot聊单元测试的分层
SpringBoot学习笔记(二)——SpringBoot测试JUnit5、 SpringBoot 配置、Spring IoC与自动装配
Spring Test与JUnit等其他测试框架结合起来,提供了便捷高效的测试手段。而Spring Boot Test 是在Spring Test之上的再次封装,增加了切片测试,增强了mock能力。
张果
2022/05/09
4.3K0
SpringBoot学习笔记(二)——SpringBoot测试JUnit5、 SpringBoot 配置、Spring IoC与自动装配
Spring Boot 2.x基础教程:事务管理入门
我们在开发企业应用时,通常业务人员的一个操作实际上是对数据库读写的多步操作的结合。由于数据操作在顺序执行的过程中,任何一步操作都有可能发生异常,异常会导致后续操作无法完成,此时由于业务逻辑并未正确的完成,之前成功操作的数据并不可靠,如果要让这个业务正确的执行下去,通常有实现方式:
程序猿DD
2020/07/14
6870
SpringBoot2.x 单元测试
我曾经在 单元测试指南 一文中写到过单元测试的必要性和 Java 单元测试相关的工具及方法。单元测试能帮助我们在早期就规避、发现和修复很多不易察觉的 bug 和漏洞,而且更能保障后期的需求变动和代码重构时所带来的隐患,减少测试成本和维护成本。在 SpringBoot2.x 集成和写单元测试更加容易了。
Abalone
2022/07/14
1.8K0
SpringBoot对单元测试支持、常用单元测试功能使用实例
Spring Boot 提供了许多注解和工具帮助开发人员测试应用,在其官方文档中也用了大量篇幅介绍单元测试的使用。在谷歌每周的 TGIF (ThanksGod, it's Friday)员工大会中有一项就是 宣布-周单元测试竞赛获胜的工程师。谷歌之所以这么重视单元测试,就是为了保证程序质量,鼓励大家多写测试代码。国内大多数开发人员对单元测试有所忽视,这也是我写本章内容的原因所在。
愿天堂没有BUG
2022/10/28
1.8K0
SpringBoot对单元测试支持、常用单元测试功能使用实例
springboot之单元测试
来源:http://www.51testing.com springboot在写完之后,肯定都需要进行单元测试,如下给出一些样例   工程层次结构如图   代码如下:   controller: pa
顾翔
2019/12/11
2940
springboot之单元测试
SpringBoot2版本Caused by: java.sql.SQLSyntaxErrorException: Table 'dinner.hibernate_sequenc
1、SpringBoot2版本Caused by: java.sql.SQLSyntaxErrorException: Table 'dinner.hibernate_sequenc报错。
别先生
2019/07/10
1.5K0
junit+mock+spring-test构建后台单元测试
    在对一些现有代码进行修改时,或者修改现有BUG的时候。都有可能对已有的代码产生影响,产生新的问题。那么怎么能避免新问题的产生呢?那就是执行回归测试,但如果是人工进行费时费力,测试的还不全面。况且一般在进度的压力下,相信很少有人会因为修改一个问题而去回归测试以前的功能。
用户2193479
2019/02/22
3.5K0
单元测试指南
在我们公司中要做单元测试,确实比较难,因为公司缺少这种氛围,有也只是局部的,大多数工程师没有这方面的习惯和素养,很多人都是有一定的抵触的心理,经过我私下的了解大概有以下几种原因吧。
Abalone
2022/07/14
6.3K0
单元测试指南
【SpringBoot】Http请求统一异常(返回数据)处理与单元测试
这个ResultUtil中的方法,其实写在BaseController中也挺不错的
谙忆
2021/01/21
8270
【SpringBoot】Http请求统一异常(返回数据)处理与单元测试
springboot使用@SpringBootTest注解进行单元测试「建议收藏」
@SpringBootTest注解是SpringBoot自1.4.0版本开始引入的一个用于测试的注解。基本用法如下:
全栈程序员站长
2022/07/29
3.1K0
一文让你了解微服务契约测试
谈到微服务,大家都想到契约测试,到底什么是契约测试呢,为什么要使用契约测试呢,关于这样的文章很多,本文将结合Spring Boot让你了解微服务契约测试。
顾翔
2024/09/10
1340
一文让你了解微服务契约测试
【从零开始】springboot单元测试(一)
工作十来年,代码也写了不少,接受过“祖传屎山”,也经历过非常优雅规范的流程,一直心里有些遗憾的,是后来绝大部分公司(不分大小)都忽略了最低成本质量保证的方法:单元测试。虽然很多公司在提,但是很少有公司愿意给程序猿分配写单元测试相应的工作量,因为这玩意表面看起来投入收益不成正比,似乎都是在做无用功,但是在产品的整个生命周期,单元测试却是产品质量的最低保证。
小尘哥
2022/12/07
4150
【从零开始】springboot单元测试(一)
一起玩转微服务(11)——一切从简单开始
使用Spring Bboot是快乐并且简单的,不需要繁琐的配置就能够完成一套非常强大的应用。
cloudskyme
2020/06/28
6640
一起玩转微服务(11)——一切从简单开始
Springboot 使用单元测试
单元测试其实是一种廉价的技术,是由开发者创建运行测试代码,用于对程序模块(软件设计的最小单位)进行正确性检验的一种做法。 而所谓的最小单元,就是指应用的最小可测试部件。 在面向对象领域,最小单元对应于类的某个成员方法。
Java3y
2019/12/05
1.2K0
Springboot 使用单元测试
推荐阅读
相关推荐
2021年 最新 多阶段构建dockerfile实现java源码编译打jar包并做成镜像
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验