This discussion is archived
3 Replies Latest reply: Feb 14, 2013 9:28 AM by TPD-Opitz-Consulting-com RSS

Mock for unit testing issue

Aacc Newbie
Currently Being Moderated
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
  • 1. Re: Mock for unit testing issue
    rp0428 Guru
    Currently Being Moderated
    >
    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.
    . . .
    as you can see, I'm not able to mock the objectC of ClassC as it not appear in my test code above.
    >
    No - we can't see that. When you don't include something in your code it means that we CANNOT see it.

    If classC is where you 'need mock' then you need to provide information about how you call 'classC', what the result of that call is and how your code uses the result of that call.. You said the method creates a connection but you didnt' say if that connection is returned and, if returned, what your code does with it.
  • 2. Re: Mock for unit testing issue
    Aacc Newbie
    Currently Being Moderated
    When I say "ClassC.methodC(), it create a connection", I just want to say I want to mock classC because it's not able to run in test context.
    however, the issue is that I can NOT mock it only because it's called at the bottom of call stack instead of an object on the top of call stack so that I can pass the mocked object in the testing code.

    If it were on the top of call stack, I really even do not need any mock tool. e.g. JMock. I could create a sub class of ClassC and provide dummy implementation of those non-testable method easily. then pass an instance of this subclass....

    Mock did not really help here .....
  • 3. Re: Mock for unit testing issue
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Aacc wrote:
    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'd say you should mock <tt>objectB</tt> not to instanciate <tt>objectC</tt> but providing dummy actions relevant to cause certain probgram flows in <tt>objectA</tt>.

    Classes <tt>B</tt> and <tt>C</tt> need tests of their own...

    bye
    TPD

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points