java - MockMailSender class automatically autowired to MailSender? -
i have implemented mockmailsender as:
@service public class mockmailsender implements mailsender { private logger log = loggerfactory.getlogger(this.getclass()); @override public void send(simplemailmessage message) throws mailexception { log.info("to: " + message.getto()); log.info("subject: " + message.getsubject()); log.info("body: " + message.gettext()); } @override public void send(simplemailmessage... messages) throws mailexception { (simplemailmessage message: messages ) { send(message); } } } good thing automatically @autowired mailsender object in @restcontroller class. when run code in testing doesn't send email mocks it. problem how can disable default behaviour if want code in production?
personally @profile approach. mark first service annotation @profile("test") , create second implementation of mailsender interface sends email itself.
@service @profile("production") public class mailsenderimpl implements mailsender { // log & send } autowire service , use it. advantage right 1 injected conditionally although there exists more beans of same interface:
@autowired mailsender mailsender; you can configure current environment (aka profile) following configuration:
@configuration public class environmentconfiguration implements webapplicationinitializer { @override public void onstartup(servletcontext sc) throws servletexception { sc.setinitparameter("spring.profiles.active", "test"); } } i not sure how spring-boot manage configuration. anyway, recommend read more @ spring docs - 25. profiles , spring-boot profiles @ mkyong's.
Comments
Post a Comment