开发手册 欢迎您!
软件开发者资料库

Spring Boot - 单元测试用例

Spring Boot Unit测试用例 - 从简单和简单的步骤学习Spring Boot,从基本到高级概念,包括简介,快速入门,Bootstrapping,Tomcat部署,构建系统,代码结构,Spring Beans和依赖注入,Runners,Application Properties,记录,构建RESTful Web服务,异常处理,拦截器,Servlet过滤器,Tomcat端口号,Rest模板,文件处理,服务组件,Thymeleaf,使用RESTful Web服务,CORS支持,国际化,调度,启用HTTPS,Eureka服务器,服务注册与Eureka,Zuul代理服务器和路由,Spring云配置服务器,Spring云配置客户端,执行器,管理服务器,管理客户端,启用Swagger2,创建Docker镜像,跟踪微服务日志,Flyway数据库,发送电子邮件,Hystrix,Web套接字,批处理服务,Apache Kafka的Spring,Twilio,单元测试用例,休息控制器单元测试,数据库处理,保护我们b应用程序,带有JWT的OAuth2,Google云平台,Google OAuth2登录。

单元测试是开发人员为确保单个单元或组件功能正常工作而进行的测试之一.

在本教程中,我们将看到如何编写使用Mockito和Web Controller进行单元测试.

Mockito

为了将Mockito Mocks注入Spring Beans,我们需要添加Mockito核心我们的构建配置文件中的依赖项.

Maven用户可以在pom.xml文件中添加以下依赖项.

   org.mockito   mockito-core   2.13.0   org.springframework.boot   spring-boot-starter-test   test

Gradle用户可以在build.gradle文件中添加以下依赖项.

compile group: 'org.mockito', name: 'mockito-core', version: '2.13.0'testCompile('org.springframework.boot:spring-boot-starter-test')

这里给出了编写Service类的代码,该类包含一个返回String值的方法.

package com.it1352.mockitodemo; import org.springframework.stereotype.Service;@Servicepublic class ProductService {   public String getProductName() {      return "Honey";   } }

现在,将ProductService类注入另一个Service类文件,如图所示.

package com.it1352.mockitodemo; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class OrderService {   @Autowired   ProductService productService;   public OrderService(ProductService productService) {      this.productService = productService;   }   public String getProductName() {      return productService.getProductName();   }}

主要的Spring Boot应用程序类文件在下面和下面给出;

package com.it1352.mockitodemo; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class MockitoDemoApplication {   public static void main(String[] args) {      SpringApplication.run(MockitoDemoApplication.class, args);   }}

然后,为测试配置应用程序上下文. @Profile("test")注释用于在测试用例运行时配置类.

package com.it1352.mockitodemo ; import org.mockito.Mockito;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.context.annotation.Profile;@Profile("test")@Configurationpublic class ProductServiceTestConfiguration {   @Bean   @Primary   public ProductService productService() {      return Mockito.mock(ProductService.class);   }}

现在,您可以在 src/test/resources下为Order Service编写单元测试用例包.

package com.it1352.mockitodemo; import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.mockito.Mockito;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.ActiveProfiles;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@SpringBootTest@ActiveProfiles("test")@RunWith(SpringJUnit4ClassRunner.class)public class MockitoDemoApplicationTests {   @Autowired   private OrderService orderService;      @Autowired   private ProductService productService;   @Test   public void whenUserIdIsProvided_thenRetrievedNameIsCorrect() {      Mockito.when(productService.getProductName()).thenReturn("Mock Product Name");      String testName = orderService.getProductName();      Assert.assertEquals("Mock Product Name", testName);   }}

下面给出了构建配置文件的完整代码.

Maven  -  pom.xml

      4.0.0   com.IT屋   mockito-demo   0.0.1-SNAPSHOT   jar   mockito-demo   Demo project for Spring Boot         org.springframework.boot      spring-boot-starter-parent      1.5.9.RELEASE                   UTF-8      UTF-8      1.8                     org.springframework.boot         spring-boot-starter                     org.mockito         mockito-core         2.13.0                     org.springframework.boot         spring-boot-starter-test         test                                       org.springframework.boot            spring-boot-maven-plugin                     

Gradle  -  build.gradle

buildscript {   ext {      springBootVersion = '1.5.9.RELEASE'   }   repositories {      mavenCentral()   }   dependencies {      classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")   }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'org.springframework.boot'group = 'com.it1352'version = '0.0.1-SNAPSHOT'sourceCompatibility = 1.8repositories {   mavenCentral()}dependencies {   compile('org.springframework.boot:spring-boot-starter')   compile group: 'org.mockito', name: 'mockito-core', version: '2.13.0'   testCompile('org.springframework.boot:spring-boot-starter-test')}

你可以创建一个可执行的JAR文件,然后运行使用以下Maven或Gradle1命令启动Spring Boot应用程序.

对于Maven,您可以使用如下所示的命令 :

mvn clean install

您可以在控制台窗口中看到测试结果.

在控制台窗口中测试结果

对于Gradle,您可以使用如下所示的命令 :

gradle clean build

您可以在控制台窗口中看到其余结果.

在控制台窗口中休息结果