Springboot 2.0.0单元测试
Posted runmove
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot 2.0.0单元测试相关的知识,希望对你有一定的参考价值。
1. 引入spring-boot-starter-test包
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.example</groupId> 7 <artifactId>java8demo</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>java8demo</name> 12 <description>Java8 Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.5.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-web</artifactId> 31 </dependency> 32 33 <dependency> 34 <groupId>org.projectlombok</groupId> 35 <artifactId>lombok</artifactId> 36 <optional>true</optional> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-starter-test</artifactId> 41 <!--<scope>test</scope>--> 42 </dependency> 43 </dependencies> 44 45 <build> 46 <plugins> 47 <plugin> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-maven-plugin</artifactId> 50 </plugin> 51 </plugins> 52 </build> 53 54 55 </project>
2. 记得在自己测试时,导包有问题的话,是要记得去掉spring-boot-starter-test中的scope范围,只需要三个注解就行
1 package com.example.java8demo; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.boot.test.context.SpringBootTest; 6 import org.springframework.test.context.junit4.SpringRunner; 7 8 import java.time.*; 9 import java.time.format.DateTimeFormatter; 10 import java.time.temporal.TemporalAdjusters; 11 import java.util.Set; 12 13 /** 14 * java 8 对于日期和时间的使用 15 * API文档:https://blog.fondme.cn/apidoc/jdk-1.8-google/下的java.time包下 16 */ 17 @RunWith(SpringRunner.class) 18 @SpringBootTest // 指定启动类 19 public class LocalDateTimeTest { 20 /** 21 * 5. DateTimeFormatter : 解析和格式化日期或时间 22 */ 23 @Test 24 public void test5(){ 25 26 DateTimeFormatter dateTimeFormater = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒"); 27 LocalDateTime localDateTime = LocalDateTime.now(); 28 System.out.println("【----未格式化之前----】" + localDateTime); 29 System.out.println("【----格式化之后----】"+dateTimeFormater.format(localDateTime)); 30 } 31 }
官方文档,可参考:https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#boot-features-testing
以上是关于Springboot 2.0.0单元测试的主要内容,如果未能解决你的问题,请参考以下文章