Home  >  Article  >  Java  >  How to skip testing in Springboot Maven packaging

How to skip testing in Springboot Maven packaging

WBOY
WBOYforward
2023-05-31 08:06:271421browse

If Maven packaging encounters unit test failure, you need to skip unit testing. In order to speed up packaging, unit testing also needs to be skipped.

Five ways to skip unit testing with Maven

To run the Springboot application in the formal environment, you need to package it first, and then use java -jar xx.jar to run us s project.

How to skip testing in Springboot Maven packaging

The database we usually use in development is the development or testing database, which is generally isolated from the production one, which means that the production configuration needs to be activated when packaging. file, but we may not have the permission to access the production library. At this time, problems will arise if we package it directly. When we directly click on the package above, it will activate the unit test. The test needs to be passed before it can be packaged. But obviously the test cannot be passed because I have activated the production configuration but I do not have permission to access the production library. This is Sometimes you will feel like you have been packaging but never finished, which requires us to skip testing when packaging. So how to skip the test? Let's discuss the solution to this problem:

1. Skip testing via command line

We can package the project by using commands and just add a command to skip testing. That's it. Use two commands to skip tests:

mvn package -DskipTests=true

  • -DskipTests=trueNo Execute the test case, but compile the test case class to generate the corresponding class file under target/test-classes.

mvn package -Dmaven.test.skip=true

  • -Dmaven.test.skip= true Does not execute test cases and does not compile test case classes.

When using mvn package for compilation and packaging, Maven will execute the JUnit test cases in src/test/java, sometimes for To skip tests, the parameters -DskipTests=true and -Dmaven.test.skip=true will be used. The main difference between these two parameters is:

Use -Dmaven.test.skip=true, not only skips the running of unit tests, but also skips the compilation of test code;
Use -DskipTests=true to skip unit tests, but Compilation will continue.

2. Configure skip testing in pom.xml

You can add the following configuration in pom.xml to skip testing:

<build>
    <plugins>
        <!-- maven 打包时跳过测试 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

3. Direct configuration of idea

The toolbar of the Maven command bar has the icon in the picture below. This icon is Skip Tests. Click to select and then use packaging in LifeStyle to skip the test. Note: Because my IDEA is the 2022 version, the icon may be slightly different from the previous version. The previous version should be a blue circle with a lightning bolt inside.

How to skip testing in Springboot Maven packaging

##4. Add Maven configuration parameters

Open the configuration and find

Build,Exxcution,Deployment –> Maven Tools –> Maven –> Runner, add -Dmaven.test.skip=true or -DskipTests=true to the VM option to skip testing during packaging.

How to skip testing in Springboot Maven packaging

5. Open the configuration by changing the settings

and find

Build,Exxcution,Deployment –> Maven Tools –> Maven &ndash ;> Runner, check the Skip Test option in Properties.

How to skip testing in Springboot Maven packaging

The above is the detailed content of How to skip testing in Springboot Maven packaging. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete