Reputation: 2488
I have my mailing service with has constructor argument and implements an interface
public class MailServiceImpl implements MailService {
private MailBo mail;
public MailBO getMail() {
return Mail;
}
public void setMail(MailBO Mail) {
this.Mail = Mail;
}
public MailServiceImpl(MailBo mail) {
throw exception if from and to address is not here
this.mail = mail;
}
public void sendMail(){
use mail object to send mail here
}
}
so now i am trying to write a test which i want to create Mailserviceimpl instance autowired with constructor argument passed in it.
i followed this link to autowire object with constructor, but i am getting below exception
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.model.MailBO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
my test class
public class MailServiceImplTest {
@Autowired
private MailService mailServiceImpl;
@Test
public void testSendValidMail(){
//test
}
}
EDIT:-
adding spring context
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.service" />
</beans>
EDIT 2:- i want my mailbo loaded with some values and inject as constructor object to mailserviceimpl
Upvotes: 1
Views: 135
Reputation: 14149
I assume that MailServiceImpl
and MailBO
are Spring Beans. Then just annotate MailServiceImpl constructor by @Autowired
and Spring will automatically injects constructor dependency
Upvotes: 1