我在这里做错了什么?我的理解是 Spring 应该按照其自动装配 EventRepository 的方式自动装配 JavaMailSender。有什么指导吗?
application.properties 和 application-test.properties
mail.host='smtp.gmail.com' -
mail.port=587
mail.username=username
mail.password=password
mail.properties.mail.smtp.starttls.enable=true
我的实现类:如果我运行我的应用程序,这很好用
@Service
public class EventService {
private EventRepository eventRepository;
private JavaMailSender javaMailSender;
public EventService(EventRepository eventRepository, JavaMailSender javaMailSender) {
this.eventRepository = eventRepository;
this.javaMailSender = javaMailSender;
}
public Event send(Event event) {
SimpleMailMessage message = new SimpleMailMessage();
message.setText("");
message.setSubject("");
message.setTo("");
message.setFrom("");
javaMailSender.send(message);
return eventRepository.save(event);
}
}
我的集成测试类:能够自动装配 EventRepository 但不能使用 JavaMailSender。
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationIntegrationTests {
@Autowired
private EventService eventService;
@Test
public void test() throws Exception {
eventService.save(new Event());
}
}
错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.javamail.**JavaMailSender**' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486)