jeudi 26 mai 2016

IllegalStateException: missing behavior definition when having a double call in same method

public class Holder() {

    Contact contact1;
    Contact contact2;
}

public class ContactServiceImpl() {

    public Contact create(Contact contact) {
        // do create
    }
}

public class HolderServiceImpl() {
    ContactService contactService = new ContactServiceImpl();

    public Holder createHolder(Holder holder) {
        contactService.create(holder.getContact1());
        contactService.create(holder.getContact2());

        return holder;
    }
}

pulbic class HolderServiceTest() {
    ContactServiceImpl contactService = new ContactServiceImpl();
    HolderServiceImpl holderService = new HolderServiceImpl();

    @Before
    public void setUp() {
        contactService = EasyMock.createMock(ContactServiceImpl.class);     
        holderService.setContactServiceImpl(contactService);
        EasyMock.expect(contactService.create(EasyMock.isA(Contact.class))).andReturn(new Contact()).anyTimes();
    }

    @Test
    public void testCreate() {
        Holder holder = new Holder();
        holder.setContact1(new Contact());
        holder.setContact2(new Contact());

        holderService.create(holder)
    }
}

When not setting the replay, I have an error on the second call of contactService.create which is IllegalStateException.

However when adding :

EasyMock.replay(contactService);

I get this error:

Unexpected method call ContactServiceImpl.create(Contact@4ebd441a[

I trying using PowerMock but I get the same issue. Is it even possible to have both calls like that? Anytimes() seems to exist for that use but it's not working much.

Aucun commentaire:

Enregistrer un commentaire