1、配置pom.xml文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、在test文件下建立test包
image.png
3、创建父test类
// SpringJUnit支持,由此引入Spring-Test框架支持!
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
//由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration
public class AppTests {
@Before
public void init(){
System.out.println("开始测试-------------------------------");
}
@After
public void after(){
System.out.println("测试结束-------------------------------");
}
}
4、建子测试类基层父测试类即可进行测试
public class WhiteIpTest extends AppTests {
@Autowired
private WhiteIpService whiteIpService;
@Test
public void insert(){
WhiteIp whiteIp = new WhiteIp();
whiteIp.setIp("192.168.0.1");
whiteIp.setWname("本地地址");
whiteIpService.addWhiteIp(whiteIp);
}
@Test
public void list(){
Page<WhiteIp> whiteIps = whiteIpService.findByPage(1,2);
for (WhiteIp w: whiteIps) {
System.out.println(w.getId());
}
System.out.println(whiteIps.getTotal()+"; "+whiteIps.getPages());
}
网友评论