在单元测试中使用反射是不好的做法吗? [英] Is it bad practice to use Reflection in Unit testing?

查看:28
本文介绍了在单元测试中使用反射是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几年我一直认为在Java中,反射在单元测试中被广泛使用.由于某些必须检查的变量/方法是私有的,因此有必要读取它们的值.一直以为Reflection API也有这个用途.

During the last years I always thought that in Java, Reflection is widely used during Unit testing. Since some of the variables/methods which have to be checked are private, it is somehow necessary to read the values of them. I always thought that the Reflection API is also used for this purpose.

上周我不得不测试一些包,因此编写了一些 JUnit 测试.和往常一样,我使用反射来访问私有字段和方法.但是我检查代码的主管对此并不满意,并告诉我反射 API 并不打算用于这种黑客攻击".相反,他建议修改生产代码中的可见性.

Last week i had to test some packages and therefore write some JUnit tests. As always i used Reflection to access private fields and methods. But my supervisor who checked the code wasn't really happy with that and told me that the Reflection API wasn't meant to use for such "hacking". Instead he suggested to modifiy the visibility in the production code.

使用反射真的不好吗?我真的不敢相信-

Is it really bad practice to use Reflection? I can't really believe that-

我应该提到我需要将所有测试都放在一个名为 test 的单独包中(因此使用受保护的可见性也不是可能的解决方案)

I should have mentioned that i was required that all tests are in a separate package called test (so using protected visibilty e.g. wasn't a possible solution too)

推荐答案

恕我直言,反射应该只是最后的手段,保留用于单元测试遗留代码或无法更改的 API 的特殊情况.如果您正在测试自己的代码,则需要使用反射的事实意味着您的设计不可测试,因此您应该修复它而不是诉诸反射.

IMHO Reflection should really only be a last resort, reserved for the special case of unit testing legacy code or an API you can't change. If you are testing your own code, the fact that you need to use Reflection means your design is not testable, so you should fix that instead of resorting to Reflection.

如果你需要在你的单元测试中访问私有成员,这通常意味着有问题的类有一个不合适的接口,和/或试图做太多.所以要么修改它的接口,要么把一些代码提取到一个单独的类中,在那里那些有问题的方法/字段访问器可以公开.

If you need to access private members in your unit tests, it usually means the class in question has an unsuitable interface, and/or tries to do too much. So either its interface should be revised, or some code should be extracted into a separate class, where those problematic methods / field accessors can be made public.

请注意,通常使用反射会导致代码除了更难理解和维护之外,也更脆弱.有一整套错误在正常情况下会被编译器检测到,但是使用反射它们只会作为运行时异常出现.

Note that using Reflection in general results in code which, apart from being harder to understand and maintain, is also more fragile. There are a whole set of errors which in the normal case would be detected by the compiler, but with Reflection they crop up as runtime exceptions only.

更新:正如@tackline 所指出的,这仅涉及在自己的测试代码中使用反射,而不涉及测试框架的内部结构.JUnit(可能还有所有其他类似的框架)使用反射来识别和调用您的测试方法——这是对反射的合理和本地化使用.如果不使用反射,就很难或不可能提供相同的功能和便利.OTOH 它完全封装在框架实现中,因此不会使我们自己的测试代码复杂化或妥协.

Update: as @tackline noted, this concerns only using Reflection within one's own test code, not the internals of the testing framework. JUnit (and probably all other similar frameworks) uses reflection to identify and call your test methods - this is a justified and localized use of reflection. It would be difficult or impossible to provide the same features and convenience without using Reflection. OTOH it is completely encapsulated within the framework implementation, so it does not complicate or compromise our own testing code.

这篇关于在单元测试中使用反射是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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