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);  }
@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); }
@Test public void shouldReturnTrueWhenCreatePersonIsNonOk() 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(true); InOrder inOrder = inOrder(personService, eventService); SampleServiceImpl sampleService = new SampleServiceImpl(personService, eventService); boolean isCreated = sampleService.createPerson("Maxime", "Nowak"); assertThat(isCreated).isFalse(); inOrder.verify(personService, times(1)).create(any(Person.class), any(BusinessMessages.class)); inOrder.verify(eventService, times(1)).sendErrorEvent(any(Person.class), eq(businessMessages)); }
@Test public void shouldThrowSampleServiceExceptionWhenPersonInstantiationFailed() throws Exception { PersonService personService = mock(PersonService.class); EventService eventService = mock(EventService.class); BusinessMessages businessMessages = mock(BusinessMessages.class); IllegalArgumentException illegalArgumentException = mock(IllegalArgumentException.class); whenNew(BusinessMessages.class).withNoArguments().thenReturn(businessMessages); whenNew(Person.class).withArguments("Maxime", "Nowak").thenThrow(illegalArgumentException); when(illegalArgumentException.getMessage()).thenReturn("Test"); SampleServiceImpl sampleService = new SampleServiceImpl(personService, eventService); try { boolean isCreated = sampleService.createPerson("Maxime", "Nowak"); fail("Should not happen !"); } catch (SampleServiceException sampleServiceException) { assertThat(sampleServiceException.getMessage()).isEqualTo("Test"); verifyZeroInteractions(personService); verifyZeroInteractions(eventService); } }