MockBean单元测试
Mr.Lee 2026-04-16 23:52:33
运行测试方法
package com.striveonger.common.third.cache.reids;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import com.striveonger.common.core.Jackson;
import com.striveonger.common.core.KeyGen;
import com.striveonger.common.core.SpringContext;
import com.striveonger.common.core.log.LogKit;
import com.striveonger.common.third.cache.config.CacheAutoConfiguration;
import ch.qos.logback.classic.Level;
/**
* RedisKit 测试
* @author Mr.Lee
* @since 2026-04-07 21:45
*/
@SpringBootTest(classes = {SpringContext.class, CacheAutoConfiguration.class})
public class RedisKitTest {
private final Logger log = LoggerFactory.getLogger(RedisKitTest.class);
@BeforeEach
public void setUp() {
try {
RedissonClient client;
Config config = Config.fromYAML("""
# 指定 DNS 解析工厂类
# addressResolverGroupFactory: !<org.redisson.connection.DnsAddressResolverGroupFactory> {}
# 线程池数量
threads: 4
# Netty线程池数量
nettyThreads: 8
singleServerConfig:
# 基础连接信息
address: "redis://192.168.194.78:6379"
password: "123456"
database: 2
# 连接池配置
connectionPoolSize: 64
connectionMinimumIdleSize: 16
# 超时与重试配置
connectTimeout: 3000 # 连接超时
timeout: 1000 # 命令等待超时
retryAttempts: 3 # 命令失败重试次数
retryInterval: 1500 # 命令重试发送时间间隔
""");
SpringContext.getBean(RedissonAutoConfigurationCustomizer.class).customize(config);
client = Redisson.create(config);
Mockito.mockStatic(SpringContext.class).when(() -> SpringContext.getBean(RedissonClient.class)).thenReturn(client);
} catch (Exception e) {
log.error("error", e);
return;
}
}
@Test
public void testSetGet() {
log.info("hello get...set...");
String key = "global:user:" + KeyGen.build("2");
String val = Jackson.builder().put("name", "tom").put("age", 18).build().toString();
// Long val = 12L;
log.info("key: {}, val: {}", key, val);
RedisKit.setValue(key, val);
Object o = RedisKit.getValue(key);
log.info("redis value: {}", o);
assert val.equals(o);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
关键代码:
@SpringBootTest(classes = {SpringContext.class, CacheAutoConfiguration.class})加载初始化 RedisKit 所需的前置对象, 交由SpringIOC管理Mockito.mockStatic(SpringContext.class).when(() -> SpringContext.getBean(RedissonClient.class)).thenReturn(client);初始化 RedisKit 工具时, 使用我们自定义的 RedissonClient
# 使用Maven测试刚写的单元测试
~/development/workspace/favor/own-commons develop +1 !4
❯ mvn test -Dsurefire.failIfNoSpecifiedTests=false -Dtest=com.striveonger.common.third.cache.reids.RedisKitTest#testSetGet
1
2
3
2
3
-Dsurefire.failIfNoSpecifiedTests=false多模块下, 要加这个此参数(不是每个模块, 都有RedisKitTest类的)