@Test public void shouldReturnTrueWhenCreatePersonIsOk() throws Exception { PersonService personService = mock(PersonService.class); EventService eventService = mock(EventService.class); BusinessMessages businessMessages = mock(BusinessMessages.class); whenNew(BusinessMessages.class).withNoArguments().thenReturn(businessMessages); when(businessMessages.hasErrors()).thenReturn(false); SampleServiceImpl sampleService = new SampleServiceImpl(personService, eventService); boolean isCreated = sampleService.createPerson("Maxime", "Nowak"); assertThat(isCreated).isTrue(); verify(personService, times(1)).create(any(Person.class), any(BusinessMessages.class)); verifyZeroInteractions(eventService); }
public boolean createPerson(String firstName, String lastName) { BusinessMessages messages = new BusinessMessages(); Person person = null; try { person = new Person(firstName, lastName); } catch (IllegalArgumentException e) { throw new SampleServiceException(e.getMessage(), e); } personService.create(person, messages); final boolean hasErrors = messages.hasErrors(); if (hasErrors) { eventService.sendErrorEvent(person, messages); } return !hasErrors; }
SampleServiceImpl implements SampleService { public boolean createPerson(String firstName, String lastName) { BusinessMessages messages = new BusinessMessages(); Person person = null; try { person = new Person(firstName, lastName); } catch (IllegalArgumentException e) { throw new SampleServiceException(e.getMessage(), e); } personService.create(person, messages); final boolean hasErrors = messages.hasErrors(); if (hasErrors) { eventService.sendErrorEvent(person, messages); } return !hasErrors; } }
SampleServiceImpl implements SampleService { public boolean createPerson(String firstName, String lastName) { BusinessMessages messages = new BusinessMessages(); Person person = null; try { person = new Person(firstName, lastName); } catch (IllegalArgumentException e) { throw new SampleServiceException(e.getMessage(), e); } personService.create(person, messages); final boolean hasErrors = messages.hasErrors(); if (hasErrors) { eventService.sendErrorEvent(person, messages); } return !hasErrors; } SampleServiceImpl(PersonService personService, EventService eventService); }
SampleServiceImpl implements SampleService { public boolean createPerson(String firstName, String lastName) { BusinessMessages messages = new BusinessMessages(); Person person = null; try { person = new Person(firstName, lastName); } catch (IllegalArgumentException e) { throw new SampleServiceException(e.getMessage(), e); } personService.create(person, messages); final boolean hasErrors = messages.hasErrors(); if (hasErrors) { eventService.sendErrorEvent(person, messages); } return !hasErrors; } SampleServiceImpl(PersonService personService, EventService eventService); boolean createPerson(String firstName, String lastName); }
SampleServiceImpl implements SampleService { public boolean createPerson(String firstName, String lastName) { BusinessMessages messages = new BusinessMessages(); Person person = null; try { person = new Person(firstName, lastName); } catch (IllegalArgumentException e) { throw new SampleServiceException(e.getMessage(), e); } personService.create(person, messages); final boolean hasErrors = messages.hasErrors(); if (hasErrors) { eventService.sendErrorEvent(person, messages); } return !hasErrors; } SampleServiceImpl(PersonService personService, EventService eventService); boolean createPerson(String firstName, String lastName);  }