Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
    Bookmark Topic Watch Topic
  • New Topic


sreenivas vemula
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Report post to moderator
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
Posts: 4549
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
Duplicate of (and nothing to do with jobs).

Can you avoid posting the same post more than once, please, and CarefullyChooseOneForum? Thanks.
    Bookmark Topic Watch Topic
  • New Topic