根据@Override public R<Employee> findById(Integer id) { String key = "ab" + id; //查询缓存 String json = (String) redisTemplate.opsForValue().get(key); if (StrUtil.isNotBlank(json)) { Employee employee = JSONUtil.toBean(json, Employee.class); log.info("从缓存查询id为" + id); return R.success(employee); } //预防缓存穿透 //命中空值 if (json != null) { log.info("缓存命中空值,值不存在"); return R.error("缓存命中空值,值不存在"); } //添加锁 //防止缓存击穿 String lockKey = "s" + id; Employee employee = null; try { //尝试获取锁 boolean b = tryLock(lockKey); //没有获得锁,重试 if (!b) { return findById(id); } //缓存不存在,查询数据库 employee = getById(id); log.info("查询数据库id为" + id); if (employee == null) { //缓存空值解决 缓存穿透 redisTemplate.opsForValue().set(key, "", Duration.ofMinutes(2)); log.info("数据库查值为空"); return R.error("数据库查值为空"); } //添加缓存 redisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(employee), Duration.ofSeconds(60)); } catch (Exception e) { throw new RuntimeException(e); } finally { //释放锁 unlock(lockKey); } return R.success(employee); } 用mockito写一个测试方法
时间: 2024-04-06 21:31:49 浏览: 149
在Java中将List转换为String输出过程解析
5星 · 资源好评率100%
好的,以下是使用Mockito编写的测试方法:
```java
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.time.Duration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.data.redis.core.RedisTemplate;
class EmployeeServiceTest {
@Mock
private RedisTemplate<String, String> redisTemplate;
private EmployeeService employeeService;
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
employeeService = new EmployeeService(redisTemplate);
}
@Test
void testFindByIdFromCache() {
int id = 1;
String key = "ab" + id;
String json = "{\"id\":1,\"name\":\"张三\",\"age\":20}";
Employee employee = new Employee();
employee.setId(id);
employee.setName("张三");
employee.setAge(20);
when(redisTemplate.opsForValue().get(key)).thenReturn(json);
R<Employee> result = employeeService.findById(id);
verify(redisTemplate).opsForValue().get(key);
assertEquals(R.success(employee), result);
}
@Test
void testFindByIdFromDB() {
int id = 1;
String key = "ab" + id;
String lockKey = "s" + id;
String json = null;
Employee employee = new Employee();
employee.setId(id);
employee.setName("张三");
employee.setAge(20);
when(redisTemplate.opsForValue().get(key)).thenReturn(json);
when(redisTemplate.opsForValue().set(key, "{\"id\":1,\"name\":\"张三\",\"age\":20}", Duration.ofSeconds(60))).thenReturn(true);
when(redisTemplate.opsForValue().set(key, "", Duration.ofMinutes(2))).thenReturn(true);
EmployeeService spyEmployeeService = mock(EmployeeService.class);
when(spyEmployeeService.tryLock(lockKey)).thenReturn(true);
when(spyEmployeeService.getById(id)).thenReturn(employee);
R<Employee> result = spyEmployeeService.findById(id);
verify(redisTemplate).opsForValue().get(key);
verify(spyEmployeeService).tryLock(lockKey);
verify(spyEmployeeService).getById(id);
verify(redisTemplate).opsForValue().set(key, "{\"id\":1,\"name\":\"张三\",\"age\":20}", Duration.ofSeconds(60));
assertEquals(R.success(employee), result);
}
}
```
该测试方法使用了`@Mock`注解和`MockitoAnnotations.initMocks(this)`,来创建`RedisTemplate`的Mock对象,并将其注入到`EmployeeService`中。然后,分别编写了两个测试用例,分别测试了从缓存中获取数据和从数据库中获取数据的情况。其中,对于从数据库中获取数据的情况,使用了`Mockito.spy()`方法,来创建`EmployeeService`的Spy对象,以便可以在测试中模拟`tryLock`和`getById`方法的行为。最后,使用`verify()`方法来验证Mock对象的方法是否被调用,使用`assertEquals()`方法来断言方法返回值是否符合预期。
阅读全文