目录
IDE:IDEA2020
JDK:JDK8
spring-boot-test:2.1.8
(1)pom
- "1.0" encoding="UTF-8"?>
"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">
-
4.0.0 -
-
org.example -
01-springboot -
1.0-SNAPSHOT -
-
-
org.springframework.boot -
spring-boot-starter-parent -
2.1.8.RELEASE -
-
-
-
-
org.springframework.boot -
spring-boot-starter -
-
-
org.springframework.boot -
spring-boot-starter-test -
test -
-
-
-
-
-
-
-
org.springframework.boot -
spring-boot-maven-plugin -
-
-
-
-
-
(2)被测类
- package com.junit.test;
-
- import org.springframework.stereotype.Service;
-
- @Service
- public class UserService {
-
- public void add(){
- System.out.println("测试add方法!");
- }
- }
(3)测试方法
- package com.junit.test;
-
-
- 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;
-
- /**
- * userService的测试类
- */
-
- @RunWith(SpringRunner.class)
- @SpringBootTest //如果与引导类包路径一致,这里可以不用写classes
- //@SpringBootTest(classes = SpringbootTestApplication.class)
- public class UserServiceTest {
- @Autowired
- private UserService userService;
-
- @Test
- public void testAdd(){
-
- userService.add();
- }
- }
(4)引导类
- package com.junit.test;
-
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class SpringbootTestApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringbootTestApplication.class,args);
- }
- }
链接:https://pan.baidu.com/s/1kqoIG2wdKkqh0M4KH859wg
提取码:vefe