New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for partial mocking #141
Comments
Can you give an example? Partials: Where you can't change the code, because it's belongs to a third party, then you could use an adaptor to thin down the third party contract to just the part you need. Privates: Before java modules you could easily test package scope methods from other same package on the test side of the code branch. This is not so easy with module rules switched on. JMock comes from world where we still believe in using interfaces. Byte code generators allowed us to mock concrete classes, but In many ways the lambda functional style has returned to using interfaces. Compose your large behaviours from smaller ones and unit test those. You'll then find you don't need to unit test privates, as you'll inject the private behaviour as a function, and functions are mockable. |
Of course you can always say "break up the code" in order to avoid the need for partial mocking. But on the other hand having a bunch of tiny classes which are only used in one single place doesn't improve code quality as well... Given this (oversimplyfied) example
Given I want to test the functionality of With partial mocking I could mock The example is very down stripped. In reality you probably have a chain of 2-3 private methods that each use mocked dependencies or do some minor modifications. Each of which would need to be inspected and the expectations would need to be defined according to the internal behaviour of these methods. By making the internal methods package private, each method could be tested separately instead of implicitly testing the internals as it is done at the moment. I know that this is not the true doctrine but this is how it works in practice - like it or not. |
The thing is, like it or not, Dependency. doSomething() is not internal, it's explicitly externally observable. If you really want to ignore the side effects of doSomething(), then you could do the following and test ClassUnderTest.toBeTestedAfterSomethingDone().
However, when I say "want to ignore the side effects of doSomething()", I feel a bit naughty.
We call this chaining a train wreck: https://devcards.io/train-wreck It's this very design that breaks encapsulation, requiring you to mock ever deeper objects and their return values. It's the design that's fragile and having to mock so many things should be the warning that the design should be improved. In practise, and only because I have good unit test coverage, I do not like this code. I would have the courage to refactor it, as I'd be confident my tests will save me from myself tomorrow, and the code will be left in a better state. JMock isn't just about unit testing for the sake of it, it's about being confident to improve code by objective measures e.g. https://en.wikipedia.org/wiki/Law_of_Demeter Mockito lets you get away with this sort of design as it fakes up return value. You may call that pragmatic, but in JMock's opinion it's sloppy. |
As said, the above example is somewhat oversimplyfied. And with chaining I was using the wrong term actually. What I meant was that some private methods of That is to test a specific aspect of However I'll take your answer as a "No" |
Correct. JMock is just being maintained, I'm not actively adding features. Mockito is much better for people adding tests retrospectively. JMock is a test first library. In our opinion, you do not end up with this type of design problem if you actually write tests first. That's the JMock library opinion, and a view expressed by the original authors here http://www.growing-object-oriented-software.com/ Day to day I use Mockito on many projects, but I personally would still refactor rather than use Mockitos thenCallRealMethod approach. If you're adding tests to existing code and you have no tests the refactoring becomes difficult. In this case Mockito is more flexible and you have to carry this technical debt with you. |
Thanks for your feedback anyways 👍 |
With the latest reanimation of JMock are there any plans to add new features to JMock like partial mocking, mocking of static methods or mocking of private methods??
Especially partial mocking would be a great thing to have!
The text was updated successfully, but these errors were encountered: