Java – avoid using the final method when using mockito for unit testing?

Mockito is not allowed to laugh at the final method

I don't want to change the implementation details just to make the test code work, but test frameworks often have these rules to encourage better coding practices

Solution

The final methods are definitely not common practices: they convey a specific message about the behavior and semantics of the method - that is, anyone who calls the method will get exactly the same implementation This is exactly what you are trying to subvert by simulating properties, because you override the final implementation by (dynamically generated) subclasses instead of executing stub behavior

For this reason, if you have an implementation that you can control if you try to replace the test with a stub, it may not be final because you treat it as a non final method in your test Your test is another user of the component, and you can design a user accordingly

In implementations beyond your control, such as third-party libraries, powermock is a generally accepted solution to simulate constructors, as well as private, static and final methods, possibly in the final class However, powermock does have some dangers:

>Simulation has additional complexity: powermock rewrites the class itself through a special class loader instead of scheduling through Java OOP methods, so you must clearly list the classes that call the final and static methods, so powermock can replace those phones. > If you do this, there is an additional risk of emulating classes that you cannot control: you may be at a disadvantage if the API or implementation changes in an incompatible way. > By violating the semantics of the final modifier, it can make your code harder to read – you declare a method with a well-defined predictable behavior, and then resolve your own declaration

In terms of "best practices", the real best practices are OOP principles. Jgitter mentioned above: "program to interfaces, not implementations." Rely on explicit interfaces:

>... you know very well the contract of the object you are interacting with. > You can freely stub and validate methods without involving the final method, test invisible methods or methods in superclasses not visible to the test The final method is particularly hidden because @ L_ 502_ 4@. >... you can easily change the implementation, including improved future implementation, mockito generated simulation, as you own, or full make implementations for testing

In other words, the simplicity of specific classes in mockito makes it an attractive choice, but please note that the simplest semantically correct answer may be to delete final from the specific methods that need stubs

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>