Injecting MailClient in while testing in Spring Boot - spring

I have this classes that runs well when I run the app.
#Service("bookManager")
public class BookManagerImpl implements BookManager {
private MailClient mailClient;
..
}
#Service
public class MailClient {
protected static final Logger looger = LoggerFactory.getLogger(MailClient.class);
#Autowired
private JavaMailSender mailSender;
private MailContentBuilder mailContentBuilder;
#Autowired
public MailClient(JavaMailSender mailSender, MailContentBuilder mailContentBuilder) {
this.mailSender = mailSender;
this.mailContentBuilder = mailContentBuilder;
}
//TODO: in a properties
public void prepareAndSend(String recipient, String message) {
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("nunito#calzada.com");
messageHelper.setTo(recipient);
messageHelper.setSubject("Sample mail subject");
String content = mailContentBuilder.build(message);
messageHelper.setText(content, true);
};
try {
if (looger.isDebugEnabled()) {
looger.debug("sending email to " + recipient);
}
mailSender.send(messagePreparator);
} catch (MailException e) {
looger.error(e.getMessage());
}
}
}
I want to create a ServiceConfig for testing purposes but I don't know how to instantiate the class MailClient because has no default constructor and the constructor MailClient() is undefined
#Configuration
public class ServiceConfig {
#Bean
public SmsService smsService() {
return new AWSSMSSender();
}
#Bean
public MailClient mailClient() {
return new MailClient();
}
}
Using the solution proposed:
return new MailClient(null, null) {
#Override
public void prepareAndSend(String recipient, String message)
{
// do nothing
}
};
I got this error:
No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}

You can user GreenMail for testing your email functionality in Spring Boot
You can refer this link : http://dolszewski.com/spring/sending-html-mail-with-spring-boot-and-thymeleaf/

It depends on what you want to do, send the email in tests (maybe with a test smpt server) or just simulate sending. In the second case you could subclass MailClient to do nothing in prepareAndSend and then use an instance of it.
return new MailClient(null, null) {
#Override
public void prepareAndSend(String recipient, String message)
{
// do nothing
}
};

You should use Stubbing.
The stubbing approach is easy to use and involves no extra dependencies for the unit test. The basic technique is to implement the collaborators as concrete classes which only exhibit the small part of the overall behaviour of the collaborator which is needed by the class under test. As an example consider the case where a service implementation is under test.

Related

Injecting spring bean (service layer class) into ResourceBundle

I created a class using ResourceBundle interface as shown below. This class is dependent on another class. The implementation class for ResourceBundle (QuestionnaireSource as shown below) always has null as dependencies. No matter if I use setter or constructor injection.
Could someone please help me with this issue. I am I missing some configuration here.
#Component
public class QuestionnaireSource extends ResourceBundle {
private final QuestionnaireCache questionnaireCache;
private static final Object lockObject = new Object();
#Override
protected Object handleGetObject(String key) {
// Gets an object for the given key from this resource bundle.
// Returns null if this resource bundle does not contain an object for the given key 0
Object value = null;
try {
value = getString(key, LocaleContextHolder.getLocale());
} catch (Exception ignored) {
}
return value;
}
public Questionnaire getString(String key, Locale locale) {
Locale l = safeLocale(locale);
return getResources(l).get(key);
}
private Locale safeLocale(Locale l) {
if (l.getLanguage().equalsIgnoreCase("DE")) {
return Locale.GERMAN;
} else {
return Locale.ENGLISH;
}
}
protected Map<String, Questionnaire> getResources(Locale locale) {
synchronized (lockObject) {
return questionnaireCache.getQuestionnaireCache().get(locale.getLanguage().toUpperCase());
}
}
#Override
public Enumeration<String> getKeys() {
return null;
}
public QuestionnaireSource(QuestionnaireCache questionnaireCache) {
super();
this.questionnaireCache = questionnaireCache;
}
}
Update:
I found that even simple dependency injection in resourceBundle is failing.
UPdate2:
The way I am using in the main class is as follows:
// ResourceBundle test here
System.out.println("Test here for resource bundle");
Locale locale = new Locale("de", "DE");
ResourceBundle bundle = ResourceBundle.getBundle("com.app.util.QuestionnaireSource", locale);
System.out.println(bundle.getString("some.test.string"));
Update3
I am writing a simple example to convey the scenario:
Some service class
#Service
public class SomeServiceTest {
public String testMethod(){
return "test here and complete";
}
}
Some example implementation of resource bundle
#Component
public class MyResourceBundle extends ResourceBundle {
private final SomeServiceTest someServiceTest;
#Autowired
public MyResourceBundle(SomeServiceTest someServiceTest) {
this.someServiceTest = someServiceTest;
}
#Override
protected Object handleGetObject(String key) {
if(key.equals("test"))
return "test";
return null;
}
#Override
public Enumeration<String> getKeys() {
return null;
}
}
Main.java
main(){
// ResourceBundle test here
System.out.println("Test here for resource bundle");
Locale locale = new Locale("de", "DE");
ResourceBundle bundle = ResourceBundle.getBundle("com.app.util.MyResourceBundle", locale);
System.out.println(bundle.getString("test"));
}
Update4:
I changed the annotation on classes as mentioned by on this post https://www.baeldung.com/spring-inject-bean-into-unmanaged-objects
but still I have the null dependency injection for SomeServiceTest class. The changes are as shown below.
SomeServiceTest.java
#Service
public class SomeServiceTest {
public String testMethod(){
return "test here and complete";
}
}
MyResourceBundle.java
#Configurable
public class MyResourceBundle extends ResourceBundle {
#Autowired
private SomeServiceTest someServiceTest;
public MyResourceBundle() {
}
#Override
protected Object handleGetObject(String key) {
if(key.equals("test"))
return someServiceTest.testMethod();
return null;
}
#Override
public Enumeration<String> getKeys() {
return null;
}
}
still SomeServiceTest class is null.
Can you please post an example on how you are using this class? Is it you (your code) or spring who instanciate it (on startup)?
#Component only works for beans which Spring instanciate. If you want to inject stuff in classes you instanciate in you code you can annotate the class with #Configurable.
Please see https://www.baeldung.com/spring-inject-bean-into-unmanaged-objects for some examples.
Make sure you have initialized the spring context
If you are using spring boot
You can get the application context after it starts and use it to get the bean you want
For example
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(YouApplication.class, args);
MyResourceBundle resConfig = run.getBean("myResourceBundle", MyResourceBundle .class);
resConfig.handleGetObject("test");
}
Unfortunately ResourceBundle.getBundle does not initialize the spring application context

Comparison of Guice and(move to) Spring

Could someone give me advice, please, how to re-write some method using simple Spring (w/o Boot)?
Here I have some code methods:
1. createInjector
private Injector injector;
someMethod(){
injector = Guice.createInjector(new ExampleClass1(), new ExampleClass2());}
2 setModules(Modules.override
setModules(Modules.override(new ExampleClass3()).with(new ExampleClass4()));
//////////////////////////////////////////////////////////////////
public static void setModules(Module... modules) {
initInjector(modules);
}
private static void initInjector(Module... modules) {
injector = Guice.createInjector(modules);
}
}
Taking the risk that my answer is too general.
Roughly saying you can think Guice modules as equivalent a configuration class with #Configuration annotation, that contains #Bean etc.
The Guice injector can be considered as equivalent to the Spring ApplicationContext.
So for example if we have two configuration files:
#Configuration
public class ConfigA {
#Bean
ExampleClass1 exampleClass1(){
return new ExampleClass1();
}
#Bean
ExampleClass2 exampleClass2(){
return new ExampleClass2();
}
}
#Configuration
public class ConfigB {
#Bean
ExampleClass1 exampleClass1(){
return new ExampleClass1();
}
#Bean
ExampleClass3 exampleClass2(){
return new ExampleClass3();
}
}
And Services ExampleClass4 that you want as alternative of ExampleClass3.
You may use the #Primary annotation
public class ExampleClass4 extends ExampleClass3 {
#Override
public String toString() {
return "ExampleClass4{}";
}
}
#Configuration
public class ConfigC {
#Bean
#Primary
ExampleClass3 exampleClass3(){
return new ExampleClass4();
}
}
So rewriting the app to Spring (core 5.2, not Spring boot) will be:
public class App {
public static void main(String[] args) {
AnnotationConfigApplicationContext ap = initAppContext();
overrideBinding(ap);
System.out.println(ap.getBean(ExampleClass3.class));
//prints ExampleClass4{}
}
private static AnnotationConfigApplicationContext initAppContext() {
AnnotationConfigApplicationContext ap = new AnnotationConfigApplicationContext();
ap.register(ConfigA.class, ConfigB.class);
return ap;
}
private static void overrideBinding(AnnotationConfigApplicationContext ap) {
ap.register(ConfigC.class);
ap.refresh();
}
}
This technic of overriding a binding will work only because ExampleClass3 wasn't defined as primary, if it doesn't that would not work and you need to consider a different approach.
For more information:
https://www.baeldung.com/spring-application-context
https://docs.spring.io/spring-javaconfig/docs/1.0.0.m3/reference/html/modularizing-configurations.html
Override bean definition in java config

spring-boot unit testing get application properties

I'm new to spring-boot, currently trying to develop a kafka producer
I want to test method that use value define in properties file. but it show value is null how solve this.I have added my property files to separate resource file in test folder also
this is my folder structure
#SpringBootTest
public class KafkaProducerImplTest {
#BeforeEach
void setUp() {
}
#Test
void check() {
KafkaProducerImpl kpi = new KafkaProducerImpl();
kpi.check();
}
}
#Service
public class KafkaProducerImpl implements KafkaProducerInterface
{
#Value("${kafka.brokers.local}")
private String kafkaBrokers;
#Value("${schema-registry}")
private String schemaRegistry;
private Properties config()
{
Properties props = new Properties();
props.setProperty("bootstrap.servers",kafkaBrokers);
props.setProperty("acks", "1");
props.setProperty("reties", "10");
props.setProperty("key.serializer", StringSerializer.class.getName());
props.setProperty("value.serializer",Serializer.class.getName());
props.setProperty("schema.registry.url",schemaRegistry);
return props;
}
public <K,T>KafkaProducer<K,T> getProducer()
{
return new KafkaProducer<>(config());
}
public <T>ProducerRecord createRecord(String Topic,T msg)
{
return new ProducerRecord<>(
Topic,msg
);
}
public void sendMessage(KafkaProducer producer,ProducerRecord record)
{
producer.send(record, (recordMetadata, e) -> {
if (e == null){
System.out.println("success");
}
});
producer.flush();
}
public void closeProducer(KafkaProducer producer){
producer.close();
}
public void check(){
System.out.println(schemaRegistry);
}
}
finally i find way, thanks everyone helping me.
#RunWith(SpringRunner.class)
#SpringBootTest
public class KafkaProducerImplTest {
#Autowired
private KafkaProducerInterface kpi;
#Test
public void check() {
kpi.check();
}
}
Annotate you test class with #RunWith(SpringRunner.class) which will load application context for you and instantiate the spring beans. To add spring boot support add #SpringBootTest(Which you already have).
You'll have to remove this line
"KafkaProducerImpl kpi = new KafkaProducerImpl();"
and autowire using interface reference instead. Something like this:
#Autowired
KafkaProducerInterface kpi;
I'm assuming you have the properties used here(ex. "kafka.brokers.local") defined in your test properties file.

Testing email services in SpringBoot

I've generated a Spring Boot web application using Spring Initializer, embedded Tomcat, Thymeleaf template engine, and package as an executable JAR file.
Technologies used:
Spring Boot 1.4.2.RELEASE, Spring 4.3.4.RELEASE, Thymeleaf 2.1.5.RELEASE, Tomcat Embed 8.5.6, Maven 3, Java 8
I have this email service that I want to test
#Service
public class MailClient {
protected static final Logger looger = LoggerFactory.getLogger(MailClient.class);
#Autowired
private JavaMailSender mailSender;
private MailContentBuilder mailContentBuilder;
#Autowired
public MailClient(JavaMailSender mailSender, MailContentBuilder mailContentBuilder) {
this.mailSender = mailSender;
this.mailContentBuilder = mailContentBuilder;
}
//TODO: in a properties
public void prepareAndSend(String recipient, String message) {
MimeMessagePreparator messagePreparator = mimeMessage -> {
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
messageHelper.setFrom("nunito#calzada.com");
messageHelper.setTo(recipient);
messageHelper.setSubject("Sample mail subject");
String content = mailContentBuilder.build(message);
messageHelper.setText(content, true);
};
try {
if (looger.isDebugEnabled()) {
looger.debug("sending email to " + recipient);
}
mailSender.send(messagePreparator);
} catch (MailException e) {
looger.error(e.getMessage());
}
}
}
I've created this test class
#RunWith(SpringRunner.class)
public class MailClientTest {
#Autowired
private MailClient mailClient;
private GreenMail smtpServer;
#Before
public void setUp() throws Exception {
smtpServer = new GreenMail(new ServerSetup(25, null, "smtp"));
smtpServer.start();
}
#Test
public void shouldSendMail() throws Exception {
//given
String recipient = "nunito.calzada#gmail.com";
String message = "Test message content";
//when
mailClient.prepareAndSend(recipient, message);
//then
String content = "<span>" + message + "</span>";
assertReceivedMessageContains(content);
}
private void assertReceivedMessageContains(String expected) throws IOException, MessagingException {
MimeMessage[] receivedMessages = smtpServer.getReceivedMessages();
assertEquals(1, receivedMessages.length);
String content = (String) receivedMessages[0].getContent();
System.out.println(content);
assertTrue(content.contains(expected));
}
#After
public void tearDown() throws Exception {
smtpServer.stop();
}
}
But I got this error when running the test
Error creating bean with name 'com.tdk.service.MailClientTest': Unsatisfied dependency expressed through field 'mailClient'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tdk.service.MailClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
The problem is that you want to run an integration test without providing the beans!
Whenever you use #Autowired, you need to provide the beans required for the autowired component feed via context.
Therefore, you need to add a static class inside that test class with #Configuration annotation. Moreover, the test class also need to know which configuration must be used via #ContextConfiguration annotation. An example here:
#RunWith(SpringRunner.class)
#ContextConfiguration(classes = {MailClientTest.ContextConfiguration.class})
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class MailClientTest {
#Configuration
#TestPropertySource(locations = "classpath:application.properties",
properties ={"my.additiona.property=123"})
#ComponentScan(basePackages = {"com.tdk.service"})
public static class ContextConfiguration {
#Bean
public JavaMailSender mailSender{
return ... //create the instance here
}
#Bean
public MailContentBuilder mailContentBuilder() {
return ... //create the instance here
}
}
}
Anyway, as I already pointed out in a comment, don't waste your time reinventing the wheel. There is already a library out of there that does all this stuff for you. I'm talking about Spring Boot Email Tools.
I think it is worth to use that library and maybe contribute to the repository with new features instead of spending time for reimplementing email support with template engines.

Spring #Autowired annotated object value is null

// My Factory class
#Component
public class UserRewardAccountValidatorFactory {
#Autowired
private VirginAmericaValidator virginAmericaValidator;
private static class SingletonHolder {
static UserRewardAccountValidatorFactory instance = new UserRewardAccountValidatorFactory();
}
public static UserRewardAccountValidatorFactory getInstance() {
return SingletonHolder.instance;
}
private UserRewardAccountValidatorFactory() {}
public PartnerValidator getPartnerValidator(Partner partner){
return virginAmericaValidator;
}
}
// My Validator class
#Service
public class VirginAmericaValidator implements PartnerValidator {
#Override
public void validate(String code) throws InvalidCodeException{
//do some processing if processing fails throw exception
if (code.equals("bad".toString())){
throw new InvalidCodeException();
}
}
}
//Usage:
PartnerValidator pv = UserRewardAccountValidatorFactory.getInstance().getPartnerValidator(partner);
if (pv != null){
try {
pv.validate(userRewardAccount);
} catch (InvalidCodeException e){
return buildResponse(ResponseStatus.INVALID_USER_REWARD_ACCOUNT, e.getMessage());
}
}
My package scan level is at much higher level. Whats happening is my virginAmericaValidator is always empty. Why is #Autowired annotation not working.
Your current approach will not work with Spring as you are ultimately using new UserRewardAccountValidatorFactory to create the instance which essentially bypasses Spring context altogether. Two approaches that should possibly work are these:
a. Using a factory-method and using xml to define your bean:
<bean class="package.UserRewardAccountValidatorFactory" name="myfactory" factory-method="getInstance"/>
This will essentially return the instance that you are creating back as a Spring bean and should get autowired cleanly.
b. Using Java #Configuration based mechanism:
#Configuration
public class MyBeanConfiguration {
#Bean
public UserRewardAccountValidatorFactory myFactory() {
return UserRewardAccountValidatorFactory.getInstance();
}
}

Resources