springboot项目 - 单元测试
引入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
选中要测试的类, Alt + Enter
出现 create Test
, 选中junit4
要在有启动类的项目中创建
注意这两个注解:
@SpringBootTest 或者 @SpringBootTest(classes = 当前启动类名.class)
@RunWith(SpringRunner.class)
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;
import static org.junit.Assert.*;
@SpringBootTest(classes = WebApplication.class)
@RunWith(SpringRunner.class)
public class DocumentPoliciesReleaseControllerTest {
@Autowired
private DocumentPoliciesReleaseService service;
@Test
public void delete() {
System.out.println("service = " + service);
}
}