一、pom文件添加spring-boot-starter-test依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
二、项目可以正常启动(maven各包正常)
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
三、编写测试类
如下图位置新建你的测试类
添加注解,启动类的名称根据自己实际情况填写
@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class)
根据需求,注入你需要的类,添加@Test注解,开始编写测试内容
package com.xxxxxx; import com.xxxx.Application; import com.xxxxxxm.util.RandomUtil; 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; @RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class UtilPay { @Autowired RandomUtil randomUtil; @Test public void util(){ System.out.println(randomUtil.generateUpperString(6)); } }
点击测试方法旁边启动按钮,进行测试,绿色表示正常
红色表示失败