SpringBoot Junit Maven JaCoCo

写一下最近写单体测试的一些笔记.

SrpingBoot的测试用例:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {ApiApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

如果需要加其他配置, 可以使用SrpingBoot的Configuration, 来引入其他资源, 比如本次就引入了excel作为配置文件.

之所以选择excel,是因为exce可以作为嵌入式数据库来用,编辑数据方便灵活,没有其他嵌入数据库可以与之相比, 可以加入vba编程, 种种好处.本想用MongDB, 还是算了,高冷难用.

-------------

mvn 命令行启动

mvn -Dtest=ApiTestSuit -DfailIfNoTests=false test
mvn test
mvn -Dtest=TestControllerTest#test -DfailIfNoTests=false test

启动目录在root pom所在目录.-Dtest=xxxx的时候, 会去每个子pom所在项目的test中找xxxx的测试, 会报错, 所以需要加上-DfailIfNoTests=false

-------------

https://blog.csdn.net/Joze_3/article/details/75402398

maven在build后会自动去Downloading 这个maven-metadata.xml文件,这个文件可以看作版本信息,作为一个版本比对。 
但maven服务器在挂了之后,会一直卡在DOWNLOADING和retry。 
解决方案: 本地的maven配置文件settings.xml

复制代码
<repositories>
    <repository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>central</id>
        <name>xxxxx-maven-virtual</name>
        <url>xxxxxxx</url>
    </repository>
    <repository>
        <id>snapshots</id>
        <name>xxx-release-maven-virtual</name>
        <url>xxxxxxx</url>
        <snapshots>
            <enabled>true</enabled>
            <!-- always,never -->
            <updatePolicy>always</updatePolicy>
        </snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>central</id>
        <name>xxx-release-maven-virtual</name>
        <url>xxxxxxxx</url>
    </pluginRepository>
    <pluginRepository>
        <id>snapshots</id>    
        <name>xxx-release-maven-virtual</name>
        <url>xxxxxx</url>
        <snapshots>
         <enabled>true</enabled>
            <!-- always,never -->
            <updatePolicy>never</updatePolicy>
        </snapshots>
    </pluginRepository>
</pluginRepositories>
复制代码

这篇讲得挺好, 简明扼要

https://www.cnblogs.com/liubaozhe/p/6929432.html

-----------------------------------------------------------------------

mvn 命令加上 -DskipTests #,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。
mvn 命令加上 -Dmaven.test.skip=true #,不执行测试用例,也不编译测试用例类。 
其中-D的意思是 -D,--define <arg> Define a system property

------------------------------------------

JaCoCo

复制代码
 <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.1</version>
    <executions>
        <execution>
            <id>prepare-agent</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>post-unit-test</id>
            <phase>test</phase>
            <goals>
                <goal>report</goal>
            </goals>
            <configuration>
                <dataFile>target/jacoco.exec</dataFile>
            </configuration>
        </execution>
    </executions>
</plugin>
复制代码

-------------------------------------------------------------

JaCoCo测试覆盖率, IDEA

https://blog.csdn.net/lvyuan1234/article/details/82836052

JaCoCo插件集成进Jekins

https://blog.csdn.net/and1kaney/article/details/51214354

JaCoCo+Maven+Jenkins自动执行单元测试并生成代码覆盖率报告

https://blog.csdn.net/supermc123/article/details/78266109

复习一下Maven的生命周期

validate
generate-sources
process-sources
generate-resources
process-resources 复制并处理资源文件,至目标目录,准备打包。
compile 编译项目的源代码。
process-classes
generate-test-sources 
process-test-sources 
generate-test-resources
process-test-resources 复制并处理资源文件,至目标测试目录。
test-compile 编译测试源代码。
process-test-classes
test 使用合适的单元测试框架运行测试。这些测试代码不会被打包或部署。
prepare-package
package 接受编译好的代码,打包成可发布的格式,如 JAR 。
pre-integration-test
integration-test
post-integration-test
verify
install 将包安装至本地仓库,以让其它项目依赖。
deploy 将最终的包复制到远程的仓库,以让其它开发人员与项目共享。

http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

猜你喜欢

转载自www.cnblogs.com/wiggins/p/12028219.html
今日推荐