我在这里做错了什么?我的理解是Spring应该以自动装配EventRepository的方式自动装配JavaMailSender。有什么指导吗?
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);
}
}
@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)
答案 0 :(得分:1)
请确保您的测试与您的主package
课程位于同一@SpringBootApplication
。
例如,如果@SpringBootApplication
类位于src/main/java/some/package
,那么您的@SpringBootTest
必须位于src/test/java/some/package
。如果不是,则需要明确设置@ComponentScan
以包含some.package
。您还可以使用@SpringBootTest(classes=...)
,@ContextConfiguration(classes=...)}
。
您还可以在测试包中放置@SpringBootConfiguration
类来扫描主包。
答案 1 :(得分:1)
在我的情况下,这是由于主资源和测试资源中的application.properties文件不同而引起的。
测试文件中缺少以下属性:
CloseableHttpClient client = HttpClients.custom()
.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public HttpUriRequest getRedirect(
HttpRequest request,
HttpResponse response,
HttpContext context) throws ProtocolException {
HttpUriRequest redirectRequest = super.getRedirect(request, response, context);
redirectRequest.addHeader("x-custom", "stuff");
return redirectRequest;
}
})
.build();
请注意,在单元测试中使用外部服务并不是最好的主意。
答案 2 :(得分:0)
自动装配规则:
我说ONLY TWO
的自动装配可能会引发null。
NEW
关键字手动创建了对象。在IoC框架环境中,应该让框架完成它的工作。例如private AnyMailSender mail = new AnyMailSender()
private JavaMailSender javaMailSender;
您应该执行类似的操作
@Autowire private JavaMailSender javaMailSender;
答案 3 :(得分:0)
您应该为 spring 自动配置定义 spring.mail.host
或 spring.mail.jndi-name
属性之一,或者在您的配置类中定义您自己的 bean。
查看该类 org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration
以了解详细信息。
示例:
@Service
public class MyEmailService {
@Autowired
private JavaMailSender mailSender;
}
import org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration;
@RunWith(SpringRunner.class)
@SpringBootTest(
properties = "spring.mail.host=localhost", // <--- inlined property
classes = {MyEmailService.class, MailSenderAutoConfiguration.class}
)
public class MyEmailServiceTest {
@Autowired
private MyEmailServiceTest emailService;
@Autowire
private JavaMailSender javaMailSender; // if you need
@Test
public void validEmails() {
// some tests of emailService
}
}
答案 4 :(得分:0)
您需要创建一个 bean。就像你的配置包中的下面一样(如果你有)。
@Bean
public JavaMailSender getJavaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("smtp.gmail.com");
mailSender.setPort(587);
mailSender.setUsername("my.gmail@gmail.com");
mailSender.setPassword("password");
Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");
return mailSender;
}
并在类上方放置@Configuration注解
答案 5 :(得分:0)
确保SpringBoot类和Test类具有相同的包名
答案 6 :(得分:0)
您的对象应该正确配置。
使用'JavaMailSenderImpl'代替,使用以下任何一种方式声明你的@Bean,
@Autowired
private JavaMailSenderImpl mailSender;
或
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();