文章目录
目录
一、springBoot相关依赖与Junit相关依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<groupId>com.sun</groupId>
<artifactId>springboot-junit</artifactId>
<version>1.0-SNAPSHOT</version>
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!-- 锁定版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
二测试类编写
1.创建配置文件application.yml文件
#端口号
server:
port: 8089
#服务名称
spring:
application:
name: junit
2.创建启动类文件
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* TODO
*
* @author xiaowensun
* @date 2023/6/11 21:47
*/
@SpringBootApplication
public class JunitApplication {
public static void main(String[] args) {
SpringApplication.run(JunitApplication.class,args);
}
}
3.创建controller 层的测试类
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* TODO
*
* @author xiaowensun
* @date 2023/6/11 21:48
*/
@RestController
@RequestMapping("/junit")
public class JunitController {
@GetMapping("/test")
public String junitTest(String test){
return test;
}
}
4.创建测试类
package com.sun.junit;
import com.sun.controller.JunitController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* TODO
*
* @author xiaowensun
* @date 2023/6/11 16:10
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSun {
@Autowired
private JunitController junitController;
@Test
public void test(){
System.out.println(junitController.junitTest("测试问题"));
}
}