Spring boot 整合Junit操作
- 步骤一 : 在pom.xml中, 添加测试启动器
<!--测试启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
-
步骤二 : 单元测试类, 添加2个注解
- @Runwith . Spring整合Junit
- @SpringBootTest , spriBoot整合Junit, 确定启动器
package com.czxy;
import com.czxy.service.AdminService;
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
//spring整合Junit
@RunWith(SpringRunner.class)
//整合spring boot 确定启动器
@SpringBootTest(classes= 启动类.class)
public class Test06Boot {
//此处可以注入任意内容
@Test
public void testDemo01(){
//使用注入内容
}
}