導航:
創建一個SpringBoot 項目+ Spring Test 測試
1. pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot程序測試依賴,如果是自動創建項目默認添加-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2 創建啓動類:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
1.3 創建Controller類
package com.example.demo.echarts;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
import static com.cdmtc.core.CdmtcECharts.createEcharts;
/**
* @program: demo
* @description:
* @author: zhang lei
* @create: 2019-11-06
*/
@RequestMapping
@RestController
public class DemoController {
@RequestMapping("sayHello")
public String sayHello(){
return "Success";
}
1.4 創建application.properties/ application.yml
這裏的話,簡單的SpringBoot項目就算啓動成功了,在配置文件中可以什麼都不用寫;啓動直接啓動DemoApplication即可;然後打開瀏覽器訪問:
localhost:8080/sayHello
如果瀏覽器返回了 “Success” 則表示項目創建成功
1.5 創建SpringBootTest 類: DemoApplicationTests.java
package com.example.demo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = DemoApplication.class)
@RunWith(SpringRunner.class)
public class DemoApplicationTests {
@Test
public void test(){
System.out.println("hello,word~");
}
}
這裏點擊運行,如果控制檯打印
hello,word~
則表示測試類創建成功;
注意,DemoApplication 必須在其他的java類前面或者同級目錄;