一、单元测试
1.1 引入依赖
1、root
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
2、具体module
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
1.2 创建测试类
@SpringBootTest // 加载Spring上下文,寻找@SpringBootApplication 注解。
public class ExcelServiceImplTest {
@Resource // 注入待测试类
private ExcelServiceImpl excelService;
}
- 在Junit4中,还需要加@RunWith(SpringRunner.class) // 实现Spring Boot单元测试
- 在JUnit 5中,Spring Boot 提供了一个扩展 @SpringBootTest,它隐式地使用 SpringExtension 来运行测试,而无需显式指定 @RunWith() 注解
版本对应关系:
1.3 编写测试方法
@SpringBootTest
class IntegrationMonitorServiceTest {
@Resource
private IntegrationMonitorService integrationMonitorService;
@Test
void testGetTpMonitorTableDTOList() {
String jsonStr = "";
List<TpDataMonitorQry> tpDataMonitorQry = JSON.parseArray(json, TpDataMonitorQry.class);
Result<List<TpMonitorInfoDTO>> tpMonitorTableDTOList = integrationMonitorService.getTpMonitorTableDTOList(tpDataMonitorQry);
Assertions.assertTrue(tpMonitorTableDTOList.getSuccess());
}
}
1.4 进阶
1、启动类
@SpringBootTest(classes = Application.class, properties = "spring.profiles.active=test")
classes 属性指向一个启动类或配置启动类,一般不填
2、feign接口调用
编写一个配置类
@SpringBootApplication(exclude = {RedisAutoConfiguration.class, RabbitAutoConfiguration.class})
@ComponentScan(basePackages = "com.gemenyaofei.integration")
@EnableLocalHostFeign(serverNameAndUrl = {ServiceNameConst.SYSTEM_SERVICE + "=http://172.16.20.56:8098"})
//@EnableLocalHostFeign(serverNameAndUrl = {ServiceNameConst.SYSTEM_SERVICE + "=http://172.16.5.132:8098"})
public class ApplicationConfiguration {
}
3、AOP/连接复用注解
(1)介绍
@BeforeEach
@BeforeAll
@AfterForeach
@AfterAll
执行顺序如下:
(2)示例
@BeforeEach
void setUp() {
System.out.println("测试开始");
}
@AfterEach
void tearDown() {
System.out.println("测试结束");
}
4、断言Assertions
Assertions.assertEquals(a,b) and Assertions.assertNotEquals()
Assertions.assertArrayEquals(Object[] expected, Object[] actual)
Assertions.assertIterableEquals(Iterable<?> expected, Iterable> actual)
Assertions.assertLinesMatch()
Assertions.assertNotNull(Object actual) and Assertions.assertNull()
Assertions.assertNotSame(originalObject, cloneObject) and Assertions.assertSame()
Assertions.assertTimeout(Duration.ofMinutes(1), () -> return "success") and Assertions.assertTimeoutPreemptively()
Assertions.assertTrue() and Assertions.assertFalse()
Assertions.assertThrows()
Assertions.fail() // 仅仅使测试用例失败
二、接口测试
2.1 定义接口
### 查询指定场景下逻辑实体的er关系,并封装成算法所需的结构
GET {{url}}/fabric/er/metabase/relation/test001
Content-Type:application/json
Cookie:{{Cookie}}
###查询客户表的字典信息,来源:ds_table_detail
POST {{url}}/metadata/exchange/pull/asset?source=testyu_oracle_51
Content-Type:application/json
Cookie:{{Cookie}}
["erp_coptc"]
### 微调er
PATCH {{url}}/fabric/er/3286495755174417787
Content-Type:application/json
Cookie:{{Cookie}}
{"entityCode":"ltc_customer_order_payment_info","tableNameList":["erp_copth","erp_coptc","erp_coptg"]}
### 更新执行状态
PUT {{url}}/metadata/executor/algorithm/status/54/3266820074312308163/3
2.2 定义环境和变量
根目录下定义http-client.env.json
{
"local": {
"url": "http://127.0.0.1:9089",
"Cookie": "",
"rp-key": "20240204151533167653-39b3ad1b-cbb3-4a80-be94-06cf62b208bf",
"er-key": "20240202151303664045-59c485db-3283-4e6e-bc08-19c5adf18720",
"er-update-key": "20240204180210881546-3b54be0a-e034-4d97-b66a-8f1743815b62"
}
}
三、MockITO
3.1 新增依赖
1、全部
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.8.5</version>
<scope>test</scope>
</dependency>
2、核心
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.7.19</version>
<scope>test</scope>
</dependency>
3.2 Mock值
1、对象
//mock对象
@MockBean
protected UserRepository mockRepository;
或者
//先创建一个mock对象
UserService userService = Mockito.mock(UserService.class);
2、when thenReturn
@Test
public void testMockBeanAnnotation() {
// 指定mock对象的行为
Mockito.when(mockRepository.count()).thenReturn(123L);
// 通过应用程序上下文获取Mock的对象
// 也可以直接调用mockRepository.count()方法
UserRepository userRepoFromContext = context.getBean(UserRepository.class);
long userCount = userRepoFromContext.count();
Assert.assertEquals(123L, userCount);
// 验证被调用的就是Mock的对象
Mockito.verify(mockRepository).count();
}
3.3 验证
1、方法调用
//验证userService的queryUser()方法是否被调用,并且参数时 "001"
Mockito.verify(userService).queryUser("001");
//上面例子,其实更加准确的说法是:验证userService的queryUser()方法得到了一次调用。相当于:
Mockito.verify(userService, Mockito.times(1)).queryUser("001");
//先创建一个mock对象
UserService userService = Mockito.mock(UserService.class);
//验证userService的queryUser()方法是否被调用,并且参数时是任意的
Mockito.verify(userService).queryUser(Mockito.any());
//验证userService的queryUser()方法是否被调用,并且参数时是任意字符串
Mockito.verify(userService).queryUser(Mockito.anyString());