有什么方法可以从 Guice 中的 Provide 中检索测试中的对象吗?
我正在尝试从模块中的 @Provides 获取 JUnit 测试中的对象,该模块不是 @Injected 因此我无法通过注入器获取它。
有什么方法可以在不注入类的情况下调用该方法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的问题缺少示例测试用例,因此很难给您准确的答案,但是,您可以使用此 junit5 扩展进行任意操作: https://github.com/exabrial/mockito-object-injection
根据示例:
如果字段与类型和名称完全匹配,则将 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:
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