应用示例:操作redis,使用断言测试
这里使用JUNIT5单元测试时自带工具类org.junit.jupiter.api.Assertions
package com.spring.demo.test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
void stringTest() {
ValueOperations valueOperations = redisTemplate.opsForValue();
if (!redisTemplate.hasKey("name")) {
valueOperations.set("name", "Jordan");
}
Assertions.assertEquals("JoRdan", valueOperations.get("name"));
}
运行后,结果执行成功显示绿色对勾,这代表结果和预期值一致
报错显示黄叉则表示结果和预期不一致,从错误信息中可以看到预期值和结果