1、引入POM
org.springframework.boot
spring-boot-starter-test
test
junit
junit
test
2、创建单元测试
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
public class test {
@Test
public void test(){
System.out.println("Helloe World");
}
}
3、注意事项
- @Test 注解为org.junit.Test包内的
- 多模块项目,测试类要创建在与web一个模块内(具体原理我也不知道,但是在别的模块跑不起来)
- test 包下面的层级关系需要与 main 下面的层级关系一样
- 如果运行测试方法运行不起来,可以尝试先运行类试试
- 测试方法不能有返回值,不能为static