Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] How to simulate save and delete from a list - general question

Milan Milanovic

2008-05-07

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi,
 
I'm new to JMock. How I can generally simulate saving and deleting from a
list using JMock in tests. Here is a simple example that I use:
 
I have a simple list:
 

final List<User> users= new ArrayList<User>();

//here I add users to this list

 
and I have a service (intefrace) that holds and works with this list:
 

inal UserService service = context.mock(UserService.class);

 

now, in my test method when method "findAll" is called I'm returning

a iterator to my list:

 

context.checking(new Expectations() {{

one(service).findAll(); will(returnValue(users));

}});

 

here I assert if I have e.g. 2 users in a list.

 

But now I want to test saving and deleting, i.e., my UserService implementation class

works with a database. How can I do this, if I want to test my save class that recieve user

as a parameter, save it to database and return the same user object:

 

final User user = new User (1, "Mark");

 

context.checking(new Expectations() {{

one(service).save(user); will(returnValue(user));

}});

 

Now, my users list should be expaded by this saved user, how can I do this ? It is the same

with deleting, I want to remove that user from a list. I supose that now when I call again findAll()

I need to have one user more in the list because this method returns all users from database.

 

--

Thx in advance, Milan Milanovic



Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
©2008 gg3721.com - Jax Systems, LLC, U.S.A.