Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Mock for unit testing issue

Aacc-OracleFeb 14 2013 — edited Feb 14 2013
I'm trying to write unit tests for my classes. there is a piece of code need to be mocked as there is no real connection can be established as testing time. however, this piece of code is at the end of my call stack.
for example
in ClassA.methodA(), it instantiate and objectB of ClassB
in ClassB.methodB(), it instantiate and objectC of ClassC
in ClassC.methodC(), it create a connection

the classC is where I need mock.

I'm using JMock

in my test, I do:
@Test
public void mytest(){
  ClassA objectA = new ClassA();
  objectA.methodA();
}
as you can see, I'm not able to mock the objectC of ClassC as it not appear in my test code above.
I learnt the sample for JMock: http://jmock.org/getting-started.html.
in that sample, the "subscriber" is an mocked object that must be passed in from the testing code instead of product code.
then, in my case, I'll have to make change to all my product code for each method mentioned above ClassA.methodA(), ClassB.methodB() to pass in ClassC objectC as parameter.
This almost impossible for my product .
is there anyway I can mock an object that does not have to be appear in testing code? (the object is instantiated in the product code, even at the bottom of call stack). if this is impossible for JMock,is there any other tool recommend?

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 14 2013
Added on Feb 14 2013
3 comments
536 views