1. 程式人生 > >Springboot 2.0.0單元測試

Springboot 2.0.0單元測試

reporting XML single apidoc schema framework docs 啟動 name

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單元測試