English 中文(简体)
What to do when Java Best Practices conflict with Mockito
原标题:
  • 时间:2010-01-12 21:30:14
  •  标签:
  • java
  • mockito

My development team has started to use Mockito and have classes that have been defined as final . I ve read in Effective Java by Joshua Bloch and in the SO thread When to use final that all classes should use the final modifier. There have been some disagreement in the thread, but I do agree with the idea of forcing class composition unless inheritance makes sense.

What should I do when I want to test classes using a testing framework like Mockito that requires classes to not have the final modifier? I m hoping someone else has encountered similar issues during their development. What resolutions did your development team arrive on?

There are two obvious answers such as using JMock or remove the final modifier on the classes we want to test, but we want to stick with one external testing framework (besides JUnit) and it may be hard to convince other developers to remove the final modifier.

Thanks.

最佳回答

What do you need most:

  1. The ability to make sure that somebody doesn t inherit from your class, OR
  2. The ability to make sure that your code is testable using your mocking framework of choice?

Generally, I believe that you don t need to enforce (1). For me, testability (2), is far more important. What fits your situation best?

问题回答

If you want your classes to be final you can have them implement interfaces. The interfaces are mockable.

As already mentioned in the other answer you can make your final classes to implement interface(s) and in your tests mock the interface(s).

This is one of the benefit of using Mock objects; in scenarios like this they make you to think about how the code can be better organized. If your code base has lot of reference to final classes (thus binding to concrete implementation) it violates the OO principle of "programming to an interface" and the need of better testability would help you to think of refactoring to eliminate dependency on concrete implementations.

This paper on usage of Mock Objects Endo-testing: Unit Testing with Mock Objects has a section (4.4) titled Interface discovery that explains how mock objects help in discovering the interfaces.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签