This discussion is archived
6 Replies Latest reply: May 5, 2010 5:50 AM by 807580 RSS

Mocking and Stubbing in Java - (JUnit, etc)

forumKid2 Explorer
Currently Being Moderated
Currently our junit tests all eventualy read the database to fill the objects and test code.

My question is, isnt it a better practice to mock/stub and avoid the trip to the database, since we are testing the logic, not the database connection?

But if my thought is right on mocking and stubbing, how can it be made possible?

For example. Lets say I have class foo which calls dataHandler.java which in turn called dataAccessor.java. There is no way that I know of that inside the foo class we could tell the dataHandler class to override the dataAccessor class and instead of using that dataAccessor which calls the database, use another class such as dataAccessorForUnitTesting and inside that class simply fill the requested object with dummy data and return it. Basically saying foo cannot override a class thats multilpe levels deep.

Just looking for some suggestions on people who use testing such as junit. If the right way is to read the database, then so be it. I'd just like someone to point that out from their experience.
  • 1. Re: Mocking and Stubbing in Java - (JUnit, etc)
    r035198x Pro
    Currently Being Moderated
    See if integrating with Jmock helps.
  • 2. Re: Mocking and Stubbing in Java - (JUnit, etc)
    807580 Newbie
    Currently Being Moderated
    ForumKid2 wrote:
    Currently our junit tests all eventualy read the database to fill the objects and test code.

    My question is, isnt it a better practice to mock/stub and avoid the trip to the database, since we are testing the logic, not the database connection?
    Depends on the nature of the test. You probably want to test that data access code works, at some point.
    But if my thought is right on mocking and stubbing, how can it be made possible?
    Hide data access behind services. That way, you can stub out those services for unit tests
  • 3. Re: Mocking and Stubbing in Java - (JUnit, etc)
    807580 Newbie
    Currently Being Moderated
    r035198x wrote:
    See if integrating with Jmock helps.
    What does that even mean?
  • 4. Re: Mocking and Stubbing in Java - (JUnit, etc)
    r035198x Pro
    Currently Being Moderated
    georgemc wrote:
    r035198x wrote:
    See if integrating with Jmock helps.
    What does that even mean?
    Well with jmock you can mock out method calls and let them return test data so you don't have to go to the DB for your test cases.
  • 5. Re: Mocking and Stubbing in Java - (JUnit, etc)
    807580 Newbie
    Currently Being Moderated
    r035198x wrote:
    georgemc wrote:
    r035198x wrote:
    See if integrating with Jmock helps.
    What does that even mean?
    Well with jmock you can mock out method calls and let them return test data so you don't have to go to the DB for your test cases.
    Yes, I know what JMock does, I just don't see that that's "integrating" with it. Isn't it just "using" JMock?

    @OP: if you're looking for a mocking library, there are far better ones than JMock around. Mockito is nice, for example. Or Powermock.
  • 6. Re: Mocking and Stubbing in Java - (JUnit, etc)
    r035198x Pro
    Currently Being Moderated
    georgemc wrote:
    r035198x wrote:
    georgemc wrote:
    r035198x wrote:
    See if integrating with Jmock helps.
    What does that even mean?
    Well with jmock you can mock out method calls and let them return test data so you don't have to go to the DB for your test cases.
    Yes, I know what JMock does, I just don't see that that's "integrating" with it. Isn't it just "using" JMock?

    @OP: if you're looking for a mocking library, there are far better ones than JMock around. Mockito is nice, for example. Or Powermock.
    Ah, the meaning was integrating it with JUnit (perhaps better plugging it into JUnit).
    I've only ever used JMock and will definitely try the ones you mentioned to be better.