File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Careers » Jobs Discussion
Reply locked New topic


sreenivas vemula
Ranch Hand

Joined: Jan 21, 2012
Posts: 44
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.

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.


where is list is some list object.

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

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

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

Joined: Apr 06, 2010
Posts: 4543

Duplicate of (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:
subject: EASY MOCK
It's not a secret anymore!