spring-boot – Spring Boot useTestClasspath抛出CannotLoadBeanClassException和ClassNotFoundException

我有一个maven项目,其测试类位于src / test /
java / MyDevClass,仅用于开发/测试目的.当我使用命令行mvn spring-boot:run启动
spring-boot-maven-plugin时,我想使用它.

所以,我的pom.xml包含:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <!-- TODO: Will create a maven profile and have useTestClasspath only for development/testing -->
                <useTestClasspath>true</useTestClasspath>
            </configuration>
        </plugin>
    </plugins>
</build>

但是,我收到以下错误:

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [MyDevClass]
Caused by: java.lang.ClassNotFoundException: MyDevClass

很有趣,我有另一个使用tomcat7-maven-plugin的项目,它工作正常:

        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <useTestClasspath>true</useTestClasspath>
            </configuration>
        </plugin>

我错过了什么?

最佳答案 即使useTestClasspath设置为true,Spring-boot maven插件也不会将当前模块的测试源(和资源)包含到类路径中.

我运行了一个带有详细日志记录的分叉执行(-X标志到Maven),插件列出了分叉的JVM类路径.测试课程不包括在内.

如果您查看插件sources(编写时的版本1.5.3),标志useTestClasspath仅用于addDependencies方法.

我看到两种选择作为解决方法

>使用 在插件配置中

<folders>
  <folder>${project.build.testOutputDirectory}</folder>
</folders>

这里的问题是文件夹被添加到类路径的开头,而测试文件最好是它.
>使用测试类和资源创建另一个Maven模块,并将其添加为< scope> test< / scope>依赖.

点赞