Junit 5 如何使用 Guice DI
Guice 是一个依赖注入的小清新工具。
相比 Spring 的依赖管理来说,这个工具更加小巧,我们可以在测试中直接使用。
Junit 5
在 Junit 中使用就没有那么方便了,因为 Junit 没有 Guice 的注解。
你需要手动写一个类,在这个类中,对 Injector 的模块进行配置。
例如我们下面的代码:
package com.ossez.wechat.oa.api.test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.ossez.wechat.common.exception.WxRuntimeException;
import com.ossez.wechat.oa.api.WeChatOfficialAccountService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatMsgService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatOfficialAccountServiceOkHttp;
import org.apache.commons.lang3.ObjectUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.BeforeAll;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.locks.ReentrantLock;
/**
* Init Guice DI
*
* @author YuCheng
*/
public class TestBase {
private static final Logger log = LoggerFactory.getLogger(TestBase.class);
private static final String TEST_CONFIG_XML = "test-config.xml";
private static final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
try (InputStream inputStream = ClassLoader.getSystemResourceAsStream(TEST_CONFIG_XML)) {
if (ObjectUtils.isEmpty(inputStream)) {
throw new WxRuntimeException("测试配置文件【" + TEST_CONFIG_XML + "】未找到,请参照test-config-sample.xml文件生成");
}
// Init WeChat config for testing
Document document = new SAXReader().read(inputStream);
TestConfigStorage config = new TestConfigStorage();
config.setAppId(document.getRootElement().element("appId").getText());
config.setSecret(document.getRootElement().element("secret").getText());
config.setToken(document.getRootElement().element("token").getText());
config.setOpenid(document.getRootElement().element("openid").getText());
config.setAccessTokenLock(new ReentrantLock());
// Init WeChat Service
WeChatOfficialAccountService weChatOfficialAccountService = new WeChatOfficialAccountServiceOkHttp();
weChatOfficialAccountService.setWxMpConfigStorage(config);
weChatOfficialAccountService.addConfigStorage("another", config);
// Init WeChatMsgService
WeChatMsgService weChatMsgService = new WeChatMsgService(weChatOfficialAccountService);
bind(TestConfigStorage.class).toInstance(config);
bind(WeChatOfficialAccountService.class).toInstance(weChatOfficialAccountService);
bind(WeChatMsgService.class).toInstance(weChatMsgService);
} catch (IOException e) {
log.error(e.getMessage(), e);
} catch (DocumentException e) {
throw new RuntimeException(e);
}
}
});
@BeforeAll
public void setup() {
injector.injectMembers(this);
}
}
在这个代码中,我们定义了一个 TestBase 的类,然后在测试启动的时候对齐进行了初始化和配置。
最主要的就是这个方法:`private static final Injector injector = Guice.createInjector(new AbstractModule() {}
在测试中使用
在测试中使用就比较简单了。
首先需要继承这个 TestBase,然后对需要的类进行注入后就可以直接使用了。
如上图,注入后直接使用。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2019-04-24 Confluence 6 针对 'unmigrated-wiki-markup' 宏重新尝试合并
2019-04-24 Confluence 6 针对合并完全失败的内容重新运行合并
2019-04-24 Confluence 6 在升级过程中查看合并日志
2019-04-24 Confluence 6 从 WIKI 标记整合到基于 XHTML 的存储格式
2018-04-24 Confluence 6 关于统一插件管理器
2018-04-24 Confluence 6 管理插件和组件
2018-04-24 Confluence 6 数据中心的 SAML 单点登录最佳实践和故障排除