Spring JUnit 测试中的 JavaMailSenderImpl 上的 ClassNotFoundException Spring Boot
ClassNotFoundException on Spring JavaMailSenderImpl in JUnit test with Spring Boot
我刚刚开始使用 Spring Boot 的新项目,我正在努力掌握它。我正在使用 Spring 工具套件,它为我创建了一个 Spring 引导项目并加载了一些我知道我需要进一步使用的依赖项(特别是 Spring 批处理和 Spring数据)
这是我的 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 数据并跳过 maven 中的测试之后,我的存根应用程序按预期运行(并查看生成的 jar,我可以看到 spring-context-support-4.3.4-RELEASE.jar 存在,包含有问题的 class),所以它似乎是一个 class 路径问题测试,但我很难过。如果我注释掉属性文件中的 spring.mail.host
行,测试运行没有问题。
如有任何想法,我们将不胜感激。
我认为我刚刚遇到的问题似乎是我的 maven 存储库中损坏的 jar 文件。我决定在我的配置 class 中手动创建 MailSender
bean 以查看是否有帮助,但是 Eclipse 将 class 标记为未找到,即使包含它的 jar 在Maven 依赖项。然后我从命令行尝试 mvn compile
,这给了我一个更有用的错误,指示损坏的罐子,所以我从我的本地存储库中删除了有问题的罐子并让 Maven 重新下载它们,现在我不再得到上面配置的错误。
希望这对其他人有帮助。
我刚刚开始使用 Spring Boot 的新项目,我正在努力掌握它。我正在使用 Spring 工具套件,它为我创建了一个 Spring 引导项目并加载了一些我知道我需要进一步使用的依赖项(特别是 Spring 批处理和 Spring数据)
这是我的 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 数据并跳过 maven 中的测试之后,我的存根应用程序按预期运行(并查看生成的 jar,我可以看到 spring-context-support-4.3.4-RELEASE.jar 存在,包含有问题的 class),所以它似乎是一个 class 路径问题测试,但我很难过。如果我注释掉属性文件中的 spring.mail.host
行,测试运行没有问题。
如有任何想法,我们将不胜感激。
我认为我刚刚遇到的问题似乎是我的 maven 存储库中损坏的 jar 文件。我决定在我的配置 class 中手动创建 MailSender
bean 以查看是否有帮助,但是 Eclipse 将 class 标记为未找到,即使包含它的 jar 在Maven 依赖项。然后我从命令行尝试 mvn compile
,这给了我一个更有用的错误,指示损坏的罐子,所以我从我的本地存储库中删除了有问题的罐子并让 Maven 重新下载它们,现在我不再得到上面配置的错误。
希望这对其他人有帮助。