@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); } }
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);  }