GuestDataProcessor implements ItemProcessor { @Override public Object processItem(Object o) throws Exception { LOGGER.info(() -> "Processing item " + o); String[] split = ((String) o).split(","); if (split.length != 3) { throw new IllegalArgumentException("Unexpected format in " + o); } String publicId = split[0].trim(); String name = split[1].trim(); String email = split[2].trim(); if (!email.matches("\\p{Alnum}+@\\p{Alnum}+.\\p{Alnum}+")) { throw new IllegalArgumentException("Unexpected email '" + email + "'"); } return new Guest(publicId, name, email); } GuestDataProcessor(); @Inject  GuestDataProcessor(JobContext jobContext); @Override Object processItem(Object o);  }
@Test public void processReturnsGuest() throws Exception { Object processItem = new GuestDataProcessor().processItem("id, foo bar, bratzen@putz.de"); Guest guest = (Guest) processItem; assertThat("foo bar", is(equalTo(guest.getName()))); assertThat("bratzen@putz.de", is(equalTo(guest.getEmail()))); }
@Test(expected = IllegalArgumentException.class) public void processThrowsExceptionIfInvalidEmail() throws Exception { new GuestDataProcessor().processItem("foo bar, invalidemail.de"); }