首页 > 编程语言 > 详细

Springboot 2.0.0单元测试

时间:2018-09-25 18:27:48      阅读:143      评论: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单元测试

原文:https://www.cnblogs.com/move22/p/9701067.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!