java - 通过 Maven 故障安全插件运行 Spring Boot 测试时出现 IllegalStateException。

标签 java maven intellij-idea spring-boot maven-failsafe-plugin

我在 IntelliJ 中创建了新的 Spring Boot 项目,并且希望将使用 Spring Boot 上下文的测试与简单的单元测试分开,因此我添加了 Maven 故障安全插件。我的配置如下所示:

        <!--RUNNING UNIT TESTS-->
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <excludes>
                    <exclude>**/*IT.java</exclude>
                </excludes>
            </configuration>
        </plugin>

        <!--RUNNING INTEGRATION TESTS-->
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

    </plugins>
</build>

我已重命名 intellij 测试类中自动生成的内容以匹配模式,测试如下所示:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ErpegApplicationTestIT {

    @Test
    public void contextLoads() {
    }

}

问题是,当我在 InttelliJ 上运行此测试时,一切正常。但是在我运行 mvn verify 后,我得到了:

[ERROR] initializationError(com.tbawor.ErpegApplicationTestIT)  Time elapsed: 0.005 s  <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @S
pringBootTest(classes=...) with your test

类命名有问题吗?我应该采取不同的方法来区分这些测试吗?

无论如何,谢谢你的帮助。

最佳答案

如果有人遇到这个问题,我已经找到了解决方案。你只需要简单地修改pom.xml:

          <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-failsafe-plugin</artifactId>
             <version>2.20</version>
             <configuration>
                 <additionalClasspathElements>
                     <additionalClasspathElement>${basedir}/target/classes</additionalClasspathElement>
                 </additionalClasspathElements>
             </configuration>
             <executions>
                 <execution>
                     <goals>
                         <goal>integration-test</goal>
                         <goal>verify</goal>
                     </goals>
                 </execution>
             </executions>
         </plugin>

关于java - 通过 Maven 故障安全插件运行 Spring Boot 测试时出现 IllegalStateException。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44226420/

相关文章:

java - Try-Catch 异常处理不提供正确的响应

android - gradle构建不包含aar android库

tomcat - 如何在intellij中配置tomcat

java - maven 获取服务器原型(prototype)失败

scala - 如何在 IntelliJ IDEA 14.0.x 中定义/安装 Scala Facet?

intellij-idea - IntelliJ IDEA 找不到 Play Twirl 模板(有时)

java - 双重验证

java - hql - 返回单个对象而不是列表?

java - 默认构造函数链接

android - 将Maven与AAR结合使用的Android项目没有Eclipse错误