简体   繁体   English

Java中的单元测试私有和静态方法

[英]Unit test private and static methods in java

I'm writing unit tests for an app of mine and as good practice I try to make all methods in my classes as private as possible, so I may end up with classes with mostly private methods, few public ones and sometimes some calls to static methods (either of my other classes or to some TextUtils, etc) 我正在为我的一个应用程序编写单元测试,并且作为一种好的实践,我尝试将类中的所有方法都尽可能地私有化,因此我可能最终得到的类大多是私有方法,很少有公共方法,有时还有些对static的调用方法(我的其他类或某些TextUtils等)

I would like to know how to get to test all of my classes trying to rely only on Mockito and JUnit since Robolectric and Powermockito seem to stretch the boundaries of what should be done in unit testing. 我想知道如何测试所有试图仅依赖Mockito和JUnit的类,因为Robolectric和Powermockito似乎扩大了单元测试中应该做的事情的界限。 Should I disregard all private and static methods along with public methods that by chance call some static or private ones? 我是否应该忽略所有私有和静态方法以及偶然会称为某些静态或私有方法的公共方法? or how? 或如何?

Note: this is general information only, as your question does not provide code to comment on. 注意:这仅是一般信息,因为您的问题没有提供要评论的代码。

Since private methods are not generally accessible outside of the class (reflection is another matter altogether) and are typically there to provide functionality to your public and protected methods, your unit test just needs to test your public and protected methods. 由于私有方法通常不能在类外部访问(完全是另一回事),并且通常在那里可以为您的公共方法和受保护方法提供功能,因此单元测试只需测试您的公共方法和受保护方法即可。 If you choose your test data carefully, you should be able to exercise most/all of the code. 如果您仔细选择测试数据,则应该能够执行大部分/全部代码。

You can use Mockito to mock out any dependencies that the class under test requires. 您可以使用Mockito来模拟被测类需要的所有依赖项。 You use expectations ( Mockito.when(...).thenReturn(...) or Mockito.verify(mockedClass).method(...) ) to mock external functionality or to check that the class under test makes outgoing calls as expected. 您可以使用期望值( Mockito.when(...).thenReturn(...)Mockito.verify(mockedClass).method(...) )来模拟外部功能或检查被测类是否以预期。

You can use assertions to check that methods being tested return appropriate values. 您可以使用断言来检查所测试的方法是否返回适当的值。

Keep in mind however, a detailed unit test with a high code coverage has a high chance of breaking the first time you try to change the internal implementation of the class under test. 但是请记住,具有高代码覆盖率的详细单元测试很可能会在您首次尝试更改被测类的内部实现时就被打破。 It's a balancing act, and you need to find the right level of coverage, while minimizing how brittle the test is. 这是一种平衡的行为,您需要找到合适的覆盖范围,同时最大程度地降低测试的脆弱性。

All of the private methods in the class you're testing should be called by some public/protected/package private method; 您要测试的类中的所有私有方法都应该由某些public / protected / package私有方法调用; otherwise they're unused code. 否则,它们是未使用的代码。 So, just concentrate on testing this public API that's visible to the "client code" of your app. 因此,只需专注于测试此公共API,该公共API对您的应用程序的“客户端代码”可见。 The internals (private methods) will get tested/covered as a side effect because they actually implement the public contract that the API specifies. 内部(私有方法)将被测试/发现为副作用,因为它们实际上实现了API指定的公共合同。

Testing implementation details (private methods) directly would make the tests harder to maintain and the code-under-test more difficult to refactor. 直接测试实现细节(私有方法)将使测试更难以维护,并且被测试代码更难以重构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM