• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

Doubt with JMock + JUnit [finding a better way to do it]

Ranch Hand
Posts: 146
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm in doubt about JMock using.
The scenario is described as follows:

I have a class implemented as a Service with EntityManager injected by spring IoC.
I'll focus on executeInvite method that uses an internal (private) method called getRemainingStatusInstance which also make using of EntityManager to recover an instance of an Entity to set a valid status for an Invitation Entity.

The problem comes when I'm using a Test class using JMock which can be viewed at a second portion of code as follows.

First portion of code (the service implementation)

And now I have the second portion of code which lists my unit test using JMock:

I'd like if I really need to do that hard work writing the whole behavior that happens at my service implementation when writing my unit test ?

Does it have a simplified way to that?

Am I writing my unit test in wrong way?

Any help will be appreciated.
Even if there isn't a straightforward answer, does any body could discuss it at this post to help me evolve the concept when using JMock?


Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Adolfo,

Thank you for sharing your code.

I´ll take a better look at it in a moment, but what I can tell you right now is that I wouldn´t mock the List class. I would just make query.getResultList() return null in the first expectation, new ArrayList<InviteStatus>() in the second and
Arrays.asList(new InviteStatusImpl()) in the third (assuming InviteStatus is an interface), because there are at least three scenarios in the private method to test.

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adolfo, Marcos,

Adolfo, I basically agree with Marcos, and I'd like to add a few points:

- Basically, you prepare your input data, let the test execute a method, and then verify the test results. If you need some services you can't easily get, then you mock them.

- Yes, sometimes there are better ways then JUnit + JMock. A few types of testing exist, mock testing is one of them. You can also test with stubs and you can also use in-container testing.

- You mock only the "outter" services. That's why you don't mock results

- JUnit and JMock are only tools. You should take some time and learn something about testing techniques, and then you'll see how the tools fit in.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic