EmailNotification implements JobExecutionListener, InitializingBean { @Override public void afterJob(JobExecution jobExecution) { logger.info("afterJob enter"); SimpleMailMessage message = new SimpleMailMessage(templateMessage); message.setSubject("Spring Batch Job Status"); message.setText("Job " + jobExecution.getJobInstance().getJobName() + " completed. Status is: " + jobExecution.getStatus()); try { mailSender.send(message); } catch (Throwable t) { logger.error("send mail failed", t); } logger.info("sent mail"); }  void setTemplateMessage(SimpleMailMessage templateMessage); void setMailSender(JavaMailSender mailSender); @Override void beforeJob(JobExecution jobExecution); @Override void afterJob(JobExecution jobExecution); @Override void afterPropertiesSet();  }
@Test public void testAfterJob() { EmailNotification notification = context.getBean("emailNotification", EmailNotification.class); JobExecution jobExecution = new JobExecution(1l); jobExecution.setJobInstance(new JobInstance(null, null, "aaa")); notification.afterJob(jobExecution); }