Mock Objects
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I was assigned the task of developing testing framework and rather than stubbing collaborators, I was tempted to use mock objects and I started looking and found JMOCK quite useful.
There was just one Publisher and SUbscriber example and I was able to successfully mock the subscriber when testing publisher.
All was well until i found that within publisher, the subscriber example explanation in EXample of publisher/subscriber there was a ADD method in publisher where we can a new Subscriber as argument value in the method signature to assign to the subscribers instance within subscriber. Meaning we would not be able to mock objects if they are created within the publisher class using new Subscriber().
Please can you help me to decide whether we can use mock objects in situations where Class under test ClassA depends on ClassB and ClassB is created within ClassA using new ClassB rather than being passed.
How to test ClassUnderTest when
Now in this situation will JMOCK or any mock object testing work when we have to mock DependentCLass?
Regards
Farouk
SCJP, SCWCD, SCBCD, SCEA 5
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
If you are allowed to change the code, it is better to refactor the code to make it more easily testable. This will make the tests simpler and the code easier to reuse.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks for answering my question but unfortunately even your answer is not working in JMOCK. Jmock expects all dependends objects be passed to the class under test and not created within classundertest(WHICH IS ESSENTIALLY THE QUESTION). I did run your test where in you have just created new DependentClass in the construtor of the Class under test rather than in the tested method.
Please can you try yourselves. Can you tell me whether we can test in such scenarios?
Thanks
Regards
Farouk
SCJP, SCWCD, SCBCD, SCEA 5
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You could use a factory:
Now you can mock the factory to create a mock.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Let me explain. I have web, business and integration tier components.
I am web developers to unit test their component using busniess stubs.
and business developers to unit test their components using integration stubs.
What i mean by stubs is instead of waiting for the real business components to be fully developed the web developers can start unit test their code by creating stub classes of the business interface and arbitarily all the methods within the stub class will return test Value objects.
This was the thaught I had but web developers have to test for each method positive and exception scenarious resulting from business layer so in order to do these test, the busienss stubbed method should have some logic to work out whether to return a positive result or a negative result based on input argument passed to it.
What if the business interface does not currently have any arguments passed to it in the method signature?
How to handle test conditions?
So i started looking at mock objects thinking it would be helpful for me to isolate test and mock data withing the test method for the mock objects
and prevent me from writing stub business interface and including complex
code withing business stubs for implementing sucess/failure situations.
Now coming back to the question.. Now I have in integraton tiers facade for DB, facade for workflow engine, facade for image repository.
Busniess components which depends on integration tier components has integration components new DBFacade(), new ImageFacade(),
new WorkFlowFacade() present within the components code. I cannot change or allowed to change the business tier component code or change there design for testing. What i have to do is provide a testing framework and also stub the calls to these facade and overriding the stub methods to return arbitary values, using which i can perform Junit test for the business components . The same is the case for web and business tier stubs
Now I m trying to find out using JMOCK orany other mock objects whether it is possible to do mock testing without writing stubs
Please help
Thanks for your understanding
Farouk
SCJP, SCWCD, SCBCD, SCEA 5
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Mohamed Farouk:
I cannot change or allowed to change the business tier component code or change there design for testing.
It seems to me than that you don't have a technical problem, but a problem with a mismatch between your responsibility and your authority.
You can't test a system that is designed to be untestable. So if you can't influence the design, you will have trouble making sure that you can test the components.
Let me repeat - from my point of view, your situation is not one where you need to just look for the right tool. You need to make sure that you get the support from management and our coworkers that you need to write tests efficiently.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
You can still use the technique I showed without changing the calling code.
There are three ways to pass the mock object in (which I left out in my original post):
1) Pass object in the constructor - Ilja described this
2) Have your test set the package private field
3) Have your test call a setter. - My personal favorite so I'll give an example for this.
The class:
The test:
While writing this, I realized that the instance variable was not an interface in my original post.
So from the point of view of production code, nothing changes. The constructor is called like it was before and creates the dependent object. Then when callDependent() is called, the real object is used by default.
From the point of view of the test, the constructor is called which again creates the dependent object. The setter is called before callDependent() to inject the mock object in. By the time callDependent() is called from the test, the mock object is in place.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I am happy that now I am able to continue the test of my components with JMOCK.
Thanks
Farouk
SCJP, SCWCD, SCBCD, SCEA 5
| I AM MIGHTY! Especially when I hold this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |










