不鼓励在同一领域使用@ Spy和@InjectMocks吗? [英] Is it discouraged to use @Spy and @InjectMocks on the same field?

查看:411
本文介绍了不鼓励在同一领域使用@ Spy和@InjectMocks吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在进行的项目中,我经常看到 @Spy @InjectMocks 已使用一起在一个领域。但我从未在任何教程或其他资源中看到过这种方式。我用Google搜索了这个特定的组合,但除了GitHub上的这个帖子之外没有找到任何其他内容:
https://github.com/mockito/mockito/issues/169



这让我觉得我们正在以一种奇怪的方式使用它。



注意:我认为同时使用两个注释的原因有时是因为如果你只使用 @InjectMocks Mockito试图使用no-args构造函数实例化该类。但是如果你没有no-args构造函数并且添加 @Spy ,你可以使用该对象而不需要空构造函数。



<编辑:另一个重要的用途是,如果你只使用两个注释,你只能存根方法。

解决方案

这是不常见的,并且可以说是不合适的,一起使用@ Spy和@InjectMocks。



@InjectMocks 作为被测系统的一种依赖注入:如果您有一个定义了正确类型的@Mock或@Spy的测试,Mockito将使用这些字段初始化@InjectMocks实例中的任何字段。如果您没有以其他方式构建您的依赖注入系统(或者如果您使用DI框架进行字段注入)并且您想要用模拟替换这些依赖项,那么这可能很方便。它可能非常脆弱 - 无法匹配的字段将被默默忽略,并且如果没有在初始化程序中设置,将保持 null - 但对于您的被测系统仍然是一个不错的注释



@Spy ,就像@Mock一样,旨在设置测试双打;当你有一个想要存根或验证的协作者时,你应该使用它。请注意,@ Spy和@Mock总是指表示依赖项,而不是您测试的系统



理想情况下,您不应该任何在同一个测试中同时履行这两个角色的类,否则你可能会发现自己正在编写一个测试,精心测试你所描述的行为,而不是实际的生产行为。在任何情况下,确切地说出测试覆盖的内容与您存在的行为相比将更加困难。



当然,如果您正在尝试,这可能不适用使用Mockito单独测试单个方法,并且您希望在测试另一个方法时将调用存根到一个方法。但是,这也可能表明您的类违反了单一责任原则,并且您应该将该类拆分为多个可以协同工作的独立类。然后,在您的测试中,您可以允许实例只有一个角色,而且不会同时需要两个注释。


In the project I'm working on right now, I often see the @Spy and @InjectMocks used together on a field. But I have never seen it this way in any tutorials or other resources. I googled after this specific combination but didn't find anything else but this thread on GitHub: https://github.com/mockito/mockito/issues/169

Which makes me think we are using it in a wierd way.

Note: The reason why I think using both annotations together makes sense sometimes is because if you only use @InjectMocks Mockito tries to instantiate the class with a no-args constructor. But if you have no no-args contructor and add @Spy you can use the object without needing an empty constructor.

Edit: Another important use is that you can only stub methods if you just use both annotations.

解决方案

It is uncommon, and arguably inappropriate, to use @Spy and @InjectMocks together.

@InjectMocks works as a sort of dependency injection for the system under test: If you have a test that defines a @Mock or @Spy of the right type, Mockito will initialize any fields in your @InjectMocks instance with those fields. This might be handy if you haven't otherwise structured your system-under-test for dependency injection (or if you use a DI framework that does field injection) and you want to replace those dependencies with mocks. It can be pretty fragile—unmatched fields will be silently ignored and will remain null if not set in an initializer—but remains a decent annotation for your system under test.

@Spy, like @Mock, is designed to set up test doubles; you should use it when you have a collaborator that you want to stub or verify. Note there that @Spy and @Mock are always meant for dependencies, and not for your system under test.

Ideally, you should not have any class that fulfills both roles in the same test, or else you may find yourself writing a test that painstakingly tests behavior that you've stubbed rather than actual production behavior. In any case it will be more difficult to tell exactly what the test covers versus the behavior you've stubbed.

Of course, this may not apply if you're trying to use Mockito to test a single method in isolation, and you want to stub calls to one method while testing the other. However, this might also be an indication that your class is violating the Single Responsibility Principle, and that you should break down the class into multiple independent classes that work together. Then, in your test, you can allow instances to have exactly one role and never need both annotations at once.

这篇关于不鼓励在同一领域使用@ Spy和@InjectMocks吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆