• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jmock query

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of jmock is not clear to me. What i have undustood so far is that it is said that it is used for mocking the objects that do not actually exist!!! Is this undustanding correct?

Consider this following scenaio.
We have to implement the functionality of login page. We so far don't have any database implementation, nor have any dao class for it.

Say the controller name is LoginController, when the user logins the controller checks whether the user already exist in the database of not with the help of databaseDao class

protected ModelAndView onSubmit(Object arg0) throws Exception {

...
String usr = databaseDao.getUserId(userName);

if (usr!=null && usr.length() > 0 ){

view="home";
}
else{
view="registration";
}

return new ModelAndView(view,"user",usr);

}


With the help of jmock can i create an dummy object of databaseDao and run the application? Or it is used just to test onSubmit method?

Can anyone please refer a tutorial for jmock to me?
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shyam kumar:
The purpose of jmock is not clear to me. What i have undustood so far is that it is said that it is used for mocking the objects that do not actually exist!!! Is this undustanding correct?


Yes. An implementation for the interface that you mock may or may not exist.

Originally posted by Shyam kumar:
With the help of jmock can i create an dummy object of databaseDao and run the application? Or it is used just to test onSubmit method?


JMock can create a mock object of the DAO for you, which you can then use for testing that the onSubmit() method does what you expect it to.

Originally posted by Shyam kumar:
Can anyone please refer a tutorial for jmock to me?


For example, there's a Getting Started tutorial on the JMock website.
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lasse,

I tried making a sample hello world program to print the output using mock object of interface Subscriber. But I'm NOT getting the expected hello world output. Is this kind of implementation correct?

import org.jmock.Mockery;
import org.jmock.Expectations;

public class Sample {
Subscriber subscriber;
public Sample(){

Mockery context = new Mockery();
subscriber = context.mock(Subscriber.class);

context.checking(
new Expectations() {
{
one (subscriber).getName("Hello");returnValue("World");
}
}
);


}
public static void main(String[] args) {
Sample sample = new Sample();
String output="";
output = sample.subscriber.getName("Hello");
System.out.println("Print==>"+output);
}
}
[ January 09, 2008: Message edited by: Shyam kumar ]
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try wrapping the "returnValue()" call with "will()" like this:

will(returnValue("World"));
 
Shyam kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lasse, I'm able to get the value printed now. you saved me from disbeliving jmock

But while doing all this thing, one thing is still bothering me;

Yes. An implementation for the interface that you mock may or may not exist.



why do we need to use jmock?

You see in the above example i could have done the same functionality as follows

TempClass implements Subscriber{

public String getName(String name){

return "World";
}

Then I could have instantiated TempClass instead of Mock class in the main calling program.

 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are some common reasons for using a test double (not just mock objects but all kinds of test doubles) instead of the real thing:
  • The real thing doesn't exist. Using a mock object lets you defer the implementation.
  • The real thing exists but is too slow. For example, the real implementation might access the network or file system, therefore adding precious milliseconds to your test execution time.
  • The real thing has access to information the test doesn't. For example, if you'd like to verify what the object under test passes on to its collaborating objects, make them mocks and have the mock objects spy for you.
  • Mock objects can simulate conditions that may not be possible to set up using the real implementations.


  • Does that make sense?
     
    Shyam kumar
    Ranch Hand
    Posts: 146
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Yes, now it is making a lot of sense to me, specially after going through the link provided by you. Thanks Lasse, you rocks
     
    He does not suffer fools gladly. But this tiny ad does:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic