@SpringBootTest不会自动装配JavaMailSender并抛出错误

时间:2017-03-05 14:05:21

标签: spring-boot javamail spring-boot-test

我在这里做错了什么?我的理解是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());
        }

        }

ERROR:

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)

7 个答案:

答案 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。

  1. 您使用NEW关键字手动创建了对象。在IoC框架环境中,应该让框架完成它的工作。

例如private AnyMailSender mail = new AnyMailSender()

  1. 某些对象可能保持非自动装配状态。如果您不是自动装配这行

private JavaMailSender javaMailSender;

您应该执行类似的操作

@Autowire private JavaMailSender javaMailSender;

答案 3 :(得分:0)

您应该为 spring 自动配置定义 spring.mail.hostspring.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();