File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other Application Frameworks and the fly likes easy mock Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Other Application Frameworks
Reply Bookmark "easy mock" Watch "easy mock" New topic
Author

easy mock

sreenivas vemula
Ranch Hand

Joined: Jan 21, 2012
Posts: 37
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
Mohamed Sanaulla
Bartender

Joined: Sep 08, 2007
Posts: 2694

Please CarefullyChooseOneForum and dont post across multiple forum.


Mohamed Sanaulla | My Blog
sreenivas vemula
Ranch Hand

Joined: Jan 21, 2012
Posts: 37
Any solution to the problem described above??
Jayesh A Lalwani
Ranch Hand

Joined: Jan 17, 2008
Posts: 301

How does class A get an object of type B. For mocking to work, you have to make sure A calls the mocked object that is created from your unit test
sreenivas vemula
Ranch Hand

Joined: Jan 21, 2012
Posts: 37
if you see the pseudo code there will be a statment for creating an object of classB in the methodA of classA.
Jayesh A Lalwani
Ranch Hand

Joined: Jan 17, 2008
Posts: 301

Well, that's exactly your problem. AFAIK, EasyMock works on code that uses some sort of Dependency Injection or Factory pattern. If you are using DI, you create the mocks yourself and then inject them into the classes you want to test. If you are using Factory pattern, you create a MockFactory that creates the mock objects. If you are instantiating classes right inside your test class, you wouldn't be able to mock it.
 
 
subject: easy mock
 
Threads others viewed
concept of method overriding
EASY MOCK
NullPointer exception
invoking super constructors.
Scope and classes/methods
MyEclipse, The Clear Choice