Help coderanch get a
new server
by contributing to the fundraiser
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin


Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Posts: 4568
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
We need your help - Coderanch server fundraiser
    Bookmark Topic Watch Topic
  • New Topic