File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Jobs Discussion and the fly likes EASY MOCK Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Careers » Jobs Discussion
Reply locked New topic
Author

EASY MOCK

sreenivas vemula
Ranch Hand

Joined: Jan 21, 2012
Posts: 44
Hi,
we are using easymock framework for mocking the java objects to reduce dependency. We got stuck at a certain point. Following is the issue we are facing.
we have two classes say classA,classB. classA has a method say methodA(). and classB has a method say methodB(). And now I want to unit test all the methods in the classA(methodA() in our example). For unit testing we are using JUNIT5. To start with the unit testing we have created a test class for the classA named TestClassA and a testmethod named testMethodA() which will return List . And methodA() in the classA will be calling methodB() of classB.

classA{
public List methodA(){
//here there will be a call to methodB of classB
}
}
class {
public List methodB(){
//here there will a code to connect to data base
}
}

As we are doing unit testing and using easy mock to reduce the dependency and hence instead of connecting to the data base (which is done by calling methodB() of classB) we are trying to mock this method call with the statement.

expect(classB.methodB()).andReturn(list);

where is list is some list object.

the code snippet is as follows:
class TestClass{
public final void testMethodA() {
List list= null;

try {
expect(classB.methodB()).andReturn(list)
list= TestClassA .methodA(); //here methodA() will be calling methodB of classB
} catch (ServiceException e) {
e.printStackTrace();
}
assertNotNull(list);

}
}
so with the first statement in try block as and when there is a call for methodB of classB , it will be replaced by some list object and there by no need to data base connectivity. But the problem is that we are mocknig the classB object in the TestClassA and call is happening in the classA. so obviously when we make a call to testmethodA() in the TestClassA , the flow will be as follows:
TestclassA().testmethodA() --> classA.methodA() --> classB.methodB() and final call classB.methodB() is going to data base. The intension is it should not go to the database ,instead it should be behaving as per the mocked object. But problem how to make the mocked object available in classA.

Please help...........

Thanks in Advance.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Duplicate of http://www.coderanch.com/t/566248/oa/EASY-MOCK (and nothing to do with jobs).

Can you avoid posting the same post more than once, please, and CarefullyChooseOneForum? Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EASY MOCK