Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring boot doesn't run unit tests

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

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.

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.

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

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

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?

like image 410
Michal W Avatar asked Oct 11 '16 17:10

Michal W


1 Answers

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.

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>

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

mvn -P test-then-run

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

like image 151
olambert Avatar answered Oct 31 '22 09:10

olambert