使用了Springboot后,就不能用原来的Spring的测试了,需要使用springboot的测试
12.1 步骤分析
1 准备一个springboot项目
略过
2 pom导入对应stater
3 写代码测试
12.2 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
12.3 写代码测试
package org.yaosang.org.service.impl;
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 org.yaosang.App;
import org.yaosang.org.domain.Department;
import org.yaosang.org.service.IDepartmentService;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class DepartmentServiceImplTest {
@Autowired
private IDepartmentService departmentService;
@Test
public void add() {
departmentService.add(new Department("屌丝部"));
}
@Test
public void del() {
Department department = departmentService.getById(1L);
System.out.println(department);
departmentService.del(1L);
department = departmentService.getById(1L);
System.out.println(department);
}
@Test
public void update() {
Department department = departmentService.getById(1L);
System.out.println(department);
department.setName(department.getName()+"-edit");
departmentService.update(department);
department = departmentService.getById(1L);
System.out.println(department);
}
@Test
public void getById() {
System.out.println(departmentService.getById(1L));
}
@Test
public void getAll() {
departmentService.getAll().forEach(System.out::println);
}
}
12.4 小结
本章节讲了Springboot-test的实现
本文介绍了如何在Spring Boot项目中进行单元测试,包括配置依赖、编写测试代码等关键步骤,并提供了具体的测试案例。
2013

被折叠的 条评论
为什么被折叠?



