java - A little guidance on jmock -


i have tried documentation on jmock site , i'm lost. appears it's great documentation knows how use it. i'm not 1 of those. i'm looking 1 of those. :)

i have services layer front ends app communicate with. services layer uses daos communicate database , return model objects. i'd use jmock daos there's no trips database.

so, have personserviceimpl implements personservice , personhibernatedao implements persondao in hibernate. example person service follows.

public class personserviceimpl implements personservice{     public void changename(int personid, string firstname, string lastname){         person = getpersondao.getperson(int personid);         person.setfirstname(firstname);         person.setlastname(lastname);         getpersondao.update(person);     } } 

how use jmock unit test person service?

i think should work.

public class mytestclass{    mockery context = new mockery();     private personserviceimpl personservice;    private persondao dao;    private person person;     @before    public void setup(){        person = new person();         // set mock return person object        dao = context.mock(persondao.class);        oneof (dao).getperson(5); will(returnvalue(person));         // create class under test mock        personservice = new personserviceimpl(dao);    }     @test    public void mytest(){      // expectations     context.checking(new expectations() {{         oneof (dao).update(person);     }});        // test       psersonservice.changename(....);      // verify     context.assertissatisfied();    } } 

personally, think mockito easier...

public class mytestclass{     private personserviceimpl personservice;    private persondao dao;    private person person;    private argumentcaptor<person> argcaptor;     @before    public void setup(){        person = new person();         argcaptor = argumentcaptor.forclass(person.class);         // set mock return person object        dao = mockito.mock(persondao.class);        when(dao.getperson(5).thenreturn(person);         // create class under test mock        personservice = new personserviceimpl(dao);    }     @test    public void mytest(){       // test       psersonservice.changename(....);        // verify       verify(dao).update(argcaptor.capture());       person passedperson = argcaptor.getvalue();       assertthat(passedperson.getfirstname(), equalto(...));    } } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -