Spring Boot 不运行单元测试 [英] Spring boot doesn't run unit tests

查看:73
本文介绍了Spring Boot 不运行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用 spring boot:run 命令构建和部署时为 spring boot 应用程序运行单元测试.

How can I run unit tests for spring boot application while building and deploying using spring boot:run command.

我的期望是在运行应用程序之前执行我所有的单元测试,但我不想在之前创建另一个像 mvn test 这样的 maven 命令.

My expectation is to have all my unit tests executed before running application, but I dont want to make another maven command like mvn test before.

我的问题:我制作了一个简单的 spring 启动应用程序,我可以找到一种方法来运行单元测试,同时从 intellij 或命令行运行应用程序.首先,我认为可能是我的测试类的配置错误或名称错误,或者项目结构错误.所以我从intellij模板创建了spring boot应用程序.令我高兴的是,它已经编写了默认测试,所以我只需运行应用程序.不幸的是没有执行测试.

My problem: I made a simple spring boot application and I could'd find a way to run unit tests while running application from intellij or from command line. Firstly I thought that maybe I have wrong configuration or wrong names of test classess or maybe wrong project structure. So I created spring boot application from intellij template. To my happiness it had already default test written so I simply run application. Unfortunatelly test was not executed.

这是intellij创建的项目结构、pom.xml、主类和单元测试截图.intetelij 创建的项目

This is a screenshot of project structure, pom.xml, main class and unit test created by intellij.Project created by intetelij

我将测试运行程序和测试更改为失败并再次尝试.结果一样.单元测试更改为失败

I changed the test runner and test to fail and tried again. Same result. unit test changed to fail

我在这里搜索了 spring boot:run 命令下面隐藏的内容http://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html

I googled what is hidden underneath spring boot:run command here http://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html

我在手册顶部发现了一些有趣的内容:在执行自身之前调用生命周期阶段 test-compile 的执行."所以我的理解是这个命令只编译测试而不运行它们?如果是,问题是 - 是否可以通过向命令添加一些标志来添加测试"阶段?

I found something interesting at the top of manual: "Invokes the execution of the lifecycle phase test-compile prior to executing itself." So my understanding is that this command only compiles tests but not run them? If So, the question is - Is it possible to add "test" phase by adding some flag to the command?

推荐答案

您的问题与 maven 生命周期有关.根据 docs 对于 spring-boot:run,默认绑定生命周期阶段validate,执行前调用test-compile阶段.

Your problem here is to do with the maven lifecycle. According to the docs for the spring-boot:run, it binds to the lifecyle phase validate by default, and invokes the phase test-compile before executing.

您要求的是在运行应用程序之前执行测试.您可以使用 POM 中的自定义 Maven 配置文件来执行此操作 - 如下所示.

What you're asking for is to execute the tests before running the application. You could do this with a custom maven profile in your POM - something like the following.

<project>
    <profiles>
        <profile>
            <id>test-then-run</id>
            <build>
                <defaultGoal>verify</defaultGoal>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>spring-boot-run</id>
                                <phase>verify</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <inherited>false</inherited>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        ...
    </profiles>
...
</project>

在你的 POM 中使用它,然后你可以运行测试并启动应用程序:

With this in your POM, you could then run the tests and start the app with:

mvn -P test-then-run

这将 run 目标绑定到 verify 阶段而不是 validate 阶段,这意味着将首先运行测试.您可以在此处查看阶段运行的顺序:https://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html

This binds the run goal to the verify phase instead of the validate phase, which means that the tests will be run first. You can see which order the phases are run here: https://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html

这篇关于Spring Boot 不运行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆