EventEmailNotifier { @Subscribe @AllowConcurrentEvents public void notifyByEmail(EventPayLoad<T> payLoad) { Preconditions.checkNotNull(payLoad); EmailContentGenerator<T> emailContentGenerator = eventEmailContentGeneratorMap.get(payLoad.getEvent()); if (emailContentGenerator != null) { EmailPayLoad emailPayLoad = emailContentGenerator.generate(payLoad); SimpleMailMessage msg = new SimpleMailMessage(); msg.setSubject(emailPayLoad.getSubject()); msg.setTo(tradingConfig.getMailTo()); msg.setText(emailPayLoad.getBody()); this.mailSender.send(msg); } else { LOG.warn("No email content generator found for event:" + payLoad.getEvent().name()); } }  @Subscribe @AllowConcurrentEvents void notifyByEmail(EventPayLoad<T> payLoad);  }
@Test @SuppressWarnings("unchecked") public void emailNotifyTest() { EventEmailNotifier<String> mailNotifier = new EventEmailNotifier<String>(); try { mailNotifier.notifyByEmail(null); fail("Null payload should have resulted in an exception"); } catch (Exception e) { } JavaMailSender mailSender = mock(JavaMailSender.class); Map<Event, EmailContentGenerator<String>> eventEmailContentGeneratorMap = Maps.newIdentityHashMap(); Event event = mock(Event.class); EmailContentGenerator<String> contentGenerator = mock(EmailContentGenerator.class); eventEmailContentGeneratorMap.put(event, contentGenerator); TradingConfig tradingConfig = mock(TradingConfig.class); mailNotifier.eventEmailContentGeneratorMap = eventEmailContentGeneratorMap; mailNotifier.mailSender = mailSender; mailNotifier.tradingConfig = tradingConfig; EventPayLoad<String> eventPayLoad = new EventPayLoad<String>(mock(Event.class), "null"); mailNotifier.notifyByEmail(eventPayLoad); verify(mailSender, times(0)).send(any(SimpleMailMessage.class)); eventPayLoad = new EventPayLoad<String>(event, "hello world"); EmailPayLoad emailPayLoad = new EmailPayLoad("hi", "test email"); when(contentGenerator.generate(eventPayLoad)).thenReturn(emailPayLoad); when(tradingConfig.getMailTo()).thenReturn("info@foobar.com"); mailNotifier.notifyByEmail(eventPayLoad); verify(mailSender, times(1)).send(any(SimpleMailMessage.class)); }