有什么方法可以从 Guice 中的 Provide 中检索测试中的对象吗?

发布于 2025-01-15 02:08:18 字数 122 浏览 5 评论 0 原文

我正在尝试从模块中的 @Provides 获取 JUnit 测试中的对象,该模块不是 @Injected 因此我无法通过注入器获取它。

有什么方法可以在不注入类的情况下调用该方法吗?

I'm trying to get a Object in a JUnit test from a @Provides in a module which isn't @Injected therefore I cannot get it by the injector.

Is there any way that I can call the method without injecting the class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对你再特殊 2025-01-22 02:08:18

由于您的问题缺少示例测试用例,因此很难给您准确的答案,但是,您可以使用此 junit5 扩展进行任意操作: https://github.com/exabrial/mockito-object-injection

根据示例:

@ExtendWith({ MockitoExtension.class, InjectExtension.class })
class MyControllerTest {
 @InjectMocks
 private MyController myController;
 @InjectionSource
 private SecurityService securityService;

如果字段与类型和名称完全匹配,则将 securityService 放入 myController 中。从那里您可以模拟该字段或提供您自己的实现

Since your question lacks an example test case, it's a bit difficult to give you an exact answer, however, you can do arbitrary with this junit5 extension: https://github.com/exabrial/mockito-object-injection

per the example:

@ExtendWith({ MockitoExtension.class, InjectExtension.class })
class MyControllerTest {
 @InjectMocks
 private MyController myController;
 @InjectionSource
 private SecurityService securityService;

will put securityService into myController if the field matches type and name exactly. From there you could either mock that field or provide your own implementation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文