Do not mock me
We all use jUnit, and I see many times developers say for to achieve proper testing for their code mocking is required as many components of their products dependent on specific environment which they can't achieve in the environment they are testing. I beg to differ.
First thing first, we are testing our product component, and if we mock specific/many sets of components how could we assure our test result is 100% correct and the code doesn't have any bug? Got this example from internet, they are mocking a Book object like below,
Come on, if you are deciding statically what you are expecting in case of specific method call how is it going to give you production level confidence?
Mock it like you didn’t
I faced same issues in my code also. Now we have to understand your requirement and specification first. Are you following TDD? Are you designing your framework from scratch or is it already designed? Are you testing a third party application?
In my case I was the sole developer for my framework, (yes once published, others are bound to use it :) ). I designed my framework in below way,
Below is the code snippet what server class does,
So here is one of mu multiple issues, I don’t have an httpSession object in my test environment. But I have designed the class such a way that I can actually extends this class and override getHttpSession() method. I do not need to override or mock the createSomething method. Below is a bad example of same class where, it is quite impossible to use my solution.
Don’t think we can’t do :) . But first thing first, design your classes such a way that minimal overriding works, and you need to override only data field/ pojos but not operations.
Now in above class I will see first in httpSession object what all fields we are using and then we will create our own httpSession. I see we are updating session attributes, for the operation to show some status in UI. So below is my httpSession (well a little),
And now I need to override the server class to use my httpSession,
So with this, we are mocking only httpSession, we are not dependent on any thirdparty jars, we are not writing multiple lines of codes to test, once written, we can use the above classes multiple times.
Private Field/methods? Use reflection
Another problem people face while testing the codes, that are badly written/legacy or if you are a tester and testing some third party code, i.e. your class is final, which is having some private methods/fields you need to call/update to make sure your test cases go fine. Simple solution use reflection (but carefully).
In below example I need to update something, which can only be updated after a web service call, again remember I am in my test environment.
Isn’t it quite simple? Feel like Supreman when you have all the controls and powers, don’t care if some open source guy suddenly changes their api.
Still can’t manage
– I understand everything, but I still can’t manage, It is a third party product, I don’t have any access and I am not sure about the reflection also, even if I know, it won’t help, I don’t have that much time to write a framework.
– Server side?
– Yes, what to do now other than mocking?
– Go to stackoverflow, read the answers in that page. See what I was saying, Cactus is good but outdated (as they told), but it is not outdated. They are not upgrading framework due to less community members. You can still use cactus from here.
– Also read this article for better technical gain and chose what you want, but don’t please don’t mock.
Agree with your point and I would like to add that just like we plan our steps before developing a software a little planning and understanding is required to have a good test framework in place.