• Post Reply 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 ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

How to dynamically mock an ArrayList using EasyMock API

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Experts,

I'm new to EasyMock JUNIT API,I got a requirement where i need to mock an arraylist in such a way that

1)Initially I need to initialize the list with some values to make sure the application logic trigger based on the List size

2)To exit out of the loop i need to dynamically mock the ArrayList size to 0 To come out of the loop.

Please share your expertise.Appreciate it..


To Illustrate

do{
arrayList = MOCKIT(ArrayList); // During here I need to mock some values to the arrayList to pass through the forEach loop
for(Object record : arrayList)
{

//PROCESS ALL RECORDS

}

arrayList = MOCKIT(ArrayList);//Here i need to mock the arrayList to 0 to come out of the do while loop.
}while(arrayList.size >0);

 
Sheriff
Posts: 16200
270
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're overthinking this. Why not just set up an actual ArrayList that contains the things you need it to contain? What do you think you are gaining by using a mock object for the ArrayList?
 
venkata Silla
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Basically,If I pass some values to the list,Lets say I inserted 2 items in the list as per the loop it never comes out since we are mocking the array list with 2 items all the time and i m looking for some mocking logic to mock the arraylist to empty once it read all the elements from the list. In other words after forEach loop i want to set the list to empty so that it will come out of the loop.

do{
arrayList = MOCKIT(ArrayList); // During here I need to mock some values to the arrayList to pass through the forEach loop
for(Object record : arrayList)
{

//PROCESS ALL RECORDS

}

arrayList = MOCKIT(ArrayList);//Here i need to mock the arrayList to 0 to come out of the do while loop.
}while(arrayList.size >0);
 
Junilu Lacar
Sheriff
Posts: 16200
270
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're going about this thing all wrong. Mocks are not meant to be used that way. Mocks are supposed to mimic the behavior of the real object. You want to use mocks when setting up a real object becomes very tedious or involves crossing over system boundaries like file systems or the network or into a database. An ArrayList is something that lives in memory so there really is no reason that I can think of to mock its behavior. If you make your Mock behave differently from how a real object would behave, then your test is INVALID.
 
Junilu Lacar
Sheriff
Posts: 16200
270
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need elements of your List to be removed as they get processed, then you should call the remove() method. Use a real ArrayList. Trying to create a mock for it is pointless.

EDIT: Looking at your code, I think a better alternative to calling remove() would be to call clear() after the for-each loop. But then again why do you have that do-while loop in the first place? What's the point of it? Please explain the logic of your test code because from what I see, that do-while part is pointless.
 
Junilu Lacar
Sheriff
Posts: 16200
270
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And please UseCodeTags (←click on that link to learn how) when you post code.
 
venkata Silla
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm sorry for the confusion around,Basically my requirment is to get all the records from the database and process it in chunks,For example 500 records at a time so the below code does that.
I'm trying to do a JUNIT Testing my mocking the List Object .Suppose,If I mock the list size to 2 elements The below program will run forever(Infinitly) I'm trying to write a JUNIT with out touching the actual code,I know list.clear()works but i dont want to touch the actual source code inorder to pass with JUNIT. Appreciate your thoughts.Thank you




 
Junilu Lacar
Sheriff
Posts: 16200
270
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's the DAO you want to mock then, not the List.
 
venkata Silla
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, Is it possible to mock the dao.getPendingRecords() to give different results

like during first visit it should return some records and In latter cases it should return 0 records
 
Junilu Lacar
Sheriff
Posts: 16200
270
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the section about Changing Behavior for the Same Method Call in the EasyMock User Guide.
 
venkata Silla
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much,Being trying to search with the keyword.It worked
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic