【发布时间】:2017-04-22 11:54:12
【问题描述】:
我刚刚使用 Spring Boot 开始了一个新项目,我正在努力掌握它。我正在使用 Spring Tool Suite,它为我创建了一个 Spring Boot 项目并加载了一些我知道我需要进一步的依赖项(特别是 Spring Batch 和 Spring Data)
这是我的 pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- JDBC driver -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
</dependencies>
我一直在阅读参考文档并且知道我会在某个时候需要邮件,所以我将它添加到我的 application.properties 文件中:
spring.mail.host=my.mail.host
然后我尝试使用mvn package 构建应用程序,但它在测试中引发了错误。测试包括以下内容:
package its.idcard.batch;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class IdCardBatchApplicationTests {
@Test
public void contextLoads() {
}
}
所以它所做的只是尝试加载应用程序上下文,但它在 org.springframework.mail.javamail.JavaMailSenderImpl 上出现 ClassNotFoundException 失败。
我知道这纯粹是测试的问题,因为在添加了一些 Spring Data 内容并在 maven 中跳过测试后,我的存根应用程序按预期运行(查看生成的 jar,我可以看到 spring- context-support-4.3.4-RELEASE.jar 存在,包含有问题的类),所以它似乎是测试中的类路径问题,但我很难过。如果我注释掉属性文件中的spring.mail.host 行,测试运行没有问题。
任何想法都将不胜感激。
【问题讨论】:
-
您需要添加
javax.mail依赖项。尝试使用这个javax.mail mail 1.4.7 -
这已经包含在
spring-boot-starter-mail依赖中,所以不需要再次添加。
标签: maven junit spring-boot jakarta-mail