Springboot 搭建测试用例
1.添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.新建test目录
右键-->>Mark Directory as --->> Test Sources Root
3.新建测试父类
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ApplicationTest {
@Before
public void init(){
System.out.println("开始测试----------");
}
@After
public void after(){
System.out.println("结束测试-----------");
}
}
4.新建测试类
测试类继承测试父类
import com.xinyou.lzkmy.ApplicationTest;
import com.xinyou.lzkmy.service.HelloService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class HelloTest extends ApplicationTest {
@Autowired
private HelloService helloService;
@Test
public void myHelloTest(){
String myHello = helloService.myHello();
System.out.println(myHello);
}
}
5.运行测试
星游