About Test Private Methods

xiaoxiao2021-03-06  123

See how the Private Methods in Java is tested

Testing Private Methods with junit and suiterunner

SO WHETHER You are using junit or suiterunner, you have the same four Basic Approaches to Testing Private Methods:

Don't Test Private Methods. Give The Methods Package Access. Use a Nested Test Class. Use reflection.

Of course, more suggestions do not test Private Methods

Approach 1: Don't test private methods

As I mentioned in the introduction, I first heard the advice to suppress my occasional urges to test private methods from Daniel Steinberg. But Daniel is not only source of this advice that I have encountered. It seems to be a common attitude in the Java community For Example, The Junit FAQ [4] States:

Testing Private Methods May Be An Indication That Those Methods SHOULD BE MOVED INTO Another Class to Private Reusability.

Charles Miller Expressed A Similar Point of View in His Weblog [5]:

If you have a thorough suite of tests for a class's exposed (non-private) interface, those tests should, by their nature, verify that any private method within the class also works. If this is not the case, or if you have A Private Method So Complex That Needs To Be Tested Out of The Context of Its Public Callers, I Would Consider That a code-smell.

And Dave Thomas and Andy Hunt, In Their Book Pragmatic Unit Testing [6], Write:

IN General, you don't want the step of testing (OR AS MOM Used to Say, "Don't Expose Your Privates!"). Most of the Time, You Should Be Able To Test A Class By EXERCISING ITS PUBLIC METHODS. IF The IS Significant Functionality That Is Hidden Behind Private Or Protected Access, That Might Be a Warning Sign That There's Another Class in There Struggling To Get Out.

I believe all this advice. Most of the time, private methods can be most effectively tested via approach 1, indirectly by testing the package-level, protected, and public methods. But inevitably, some people in some situations will feel that that call them directly testing a private method is the right thing to do.In my case, I tend to create many private utility methods. These utility methods often do nothing with instance data, they just operate on the passed parameters and return a result. I create such methods to make the calling method easier to understand. It is a way to manage the complexity of the implementation of the class. Now, if I extract the private method out of a method that already works and has good unit test coverage, then those existing unit tests will likely suffice. I need not write more unit tests just for the private method. But if I want to write the private method before its calling method, and I want to write the unit tests before writing the private method, I 'M back to wanting to directly test the private method. In the case of private utility methods, I do not feel my urge to directly test the methods is, as the JUnit FAQ put it, "an indication that those methods should be moved INTO Another Class to Promote Reusability. "" The Methods Are Really Only Really, And in Fact Are Fact Are Off.

转载请注明原文地址:https://www.9cbs.com/read-125548.html

New Post(0)