Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Testing and the fly likes jmock query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "jmock query" Watch "jmock query" New topic
Author

jmock query

Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
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?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Try wrapping the "returnValue()" call with "will()" like this:

will(returnValue("World"));
Shyam kumar
Ranch Hand

Joined: May 21, 2006
Posts: 146
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
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

    Joined: May 21, 2006
    Posts: 146
    Yes, now it is making a lot of sense to me, specially after going through the link provided by you. Thanks Lasse, you rocks
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: jmock query