Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lsiu/6c87f4e96408929ed263 to your computer and use it in GitHub Desktop.
Save lsiu/6c87f4e96408929ed263 to your computer and use it in GitHub Desktop.
Example of manually bootstraping SpringBoot AutoConfiguration with conditions depending on existance of spring boot properties
@Test
void "can send email without errors"() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()
StandardEnvironment environment = new StandardEnvironment()
InputStream ins = Resources.getResourceAsStream("/application.properties")
Properties props = new Properties()
props.load(ins as InputStream);
Closeables.closeQuietly(ins);
environment.propertySources.addFirst(new PropertiesPropertySource("application", props))
context.setEnvironment(environment)
context.register(MailSenderAutoConfiguration)
context.refresh()
context.start()
println context.getBean(MailerTest).host
JavaMailSender mailSender = context.getBean(JavaMailSender)
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(new ClassLoaderTemplateResolver(prefix: "templates/", templateMode: "HTML5", order: 1))
Mailer mailer = new Mailer(mailSender, templateEngine)
mailer.merchantCreated(new Merchant(merchantName: "Test Merchant Name", contactName: "Contact Name", emailAddress: "leonard.siu@trioloop.com"))
println "Method returned"
ForkJoinPool.commonPool().awaitQuiescence(30, TimeUnit.SECONDS)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment