zoukankan      html  css  js  c++  java
  • 【Junit】Junit 与 SpringBoot 整合测试

      本章介绍使用 Junit4 测试 SpringBoot,关于 Junit4 的基本使用 参考:【Junit】Junit快速入门

      关于SpringBoot的使用,参考:【SpringBoot】SpringBoot快速入门(一)

    一、项目框架

    1、搭建一个Maven项目,引入 springboot依赖 和 junit依赖

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <project xmlns="http://maven.apache.org/POM/4.0.0"
     3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     5     <modelVersion>4.0.0</modelVersion>
     6 
     7     <groupId>com.test.junit</groupId>
     8     <artifactId>test-junit-springboot</artifactId>
     9     <version>1.0-SNAPSHOT</version>
    10 
    11     <parent>
    12         <groupId>org.springframework.boot</groupId>
    13         <artifactId>spring-boot-starter-parent</artifactId>
    14         <version>2.1.8.RELEASE</version>
    15     </parent>
    16 
    17     <properties>
    18         <maven.compiler.source>8</maven.compiler.source>
    19         <maven.compiler.target>8</maven.compiler.target>
    20     </properties>
    21 
    22     <dependencies>
    23         <dependency>
    24             <groupId>org.springframework.boot</groupId>
    25             <artifactId>spring-boot-starter</artifactId>
    26         </dependency>
    27         <dependency>
    28             <groupId>org.springframework.boot</groupId>
    29             <artifactId>spring-boot-starter-test</artifactId>
    30             <scope>test</scope>
    31         </dependency>
    32     </dependencies>
    33 
    34 
    35     <build>
    36         <plugins>
    37             <plugin>
    38                 <groupId>org.springframework.boot</groupId>
    39                 <artifactId>spring-boot-maven-plugin</artifactId>
    40                 <version>2.1.8.RELEASE</version>
    41             </plugin>
    42         </plugins>
    43     </build>
    44 
    45 </project>

    2、新建一个配置类

     1 package com.test.junit.springboot.config;
     2 
     3 import org.springframework.context.annotation.Bean;
     4 import org.springframework.context.annotation.Configuration;
     5 
     6 import java.util.Date;
     7 
     8 @Configuration
     9 public class MyConfig {
    10     @Bean
    11     public Date date(){
    12         return new Date();
    13     }
    14 }

    3、SpringBoot项目入口

     1 package com.test.junit.springboot;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.context.ConfigurableApplicationContext;
     6 
     7 @SpringBootApplication
     8 public class Application {
     9     public static void main(String[] args) {
    10         SpringApplication.run(Application.class);
    11     }
    12 }

    二、使用Junit4 测试 SpringBoot

      新建一个测试类

     1 package com.test.junit.springboot;
     2 
     3 import org.junit.Test;
     4 import org.junit.runner.RunWith;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.boot.test.context.SpringBootTest;
     7 import org.springframework.context.ApplicationContext;
     8 import org.springframework.test.context.junit4.SpringRunner;
     9 
    10 import java.util.Date;
    11 
    12 @RunWith(SpringRunner.class)
    13 @SpringBootTest
    14 public class TestApplication {
    15 
    16     @Autowired
    17     ApplicationContext context;
    18 
    19     @Test
    20     public void contextLoads() {
    21         Date date = (Date) context.getBean("date");
    22         System.out.println(date);
    23     }
    24 } 
  • 相关阅读:
    delphi 数据导出 进度条自己生成
    在存储过程中编写正确的事务处理代码(SQL Server 2000 & 2005)
    在Delphi中如何获得SQL中存储过程的返回值?
    object-c中管理文件和目录:NSFileManager使用方法
    Delphi中动态链接库(DLL)的建立和使用
    开源免费天气预报接口API以及全国所有地区代码!!(国家气象局提供)
    wince 程序无法执行的原因
    如果我写一个播放器
    busybox配置
    switch case被人忽视的一点!zt
  • 原文地址:https://www.cnblogs.com/h--d/p/14702872.html
Copyright © 2011-2022 走看看