1. 程式人生 > >Maven之Maven Surefire Plugin(JUnit篇)

Maven之Maven Surefire Plugin(JUnit篇)

1.maven-surefire-plugin是個什麼鬼?

如果你執行過mvn test或者執行其他maven命令時跑了測試用例,你就已經用過maven-surefire-plugin了。maven-surefire-plugin是maven裡執行測試用例的外掛,不顯示配置就會用預設配置。這個外掛的surefire:test命令會預設繫結maven執行的test階段。

maven的生命週期有哪些階段?

[validate, initialize, 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, pre-integration-test, integration-test, post-integration-test, verify, install, deploy]

2.maven-surefire-plugin的使用

如果說maven已經有了maven-surefire-plugin的預設配置,我們還有必要了解maven-surefire-plugin的配置麼?答案是肯定的。雖說maven-surefire-plugin有預設配置,但是當需要修改一些測試執行的策略時,就有必要我們去重新配置這個外掛了。

2.1.配置JUnit

2.1.1.外掛自動匹配

最簡單的配置方式就不配置或者是隻宣告外掛。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
</plugin>

這個時候maven-surefire-plugin會按照如下邏輯去尋找JUnit的版本並執行測試用例。

if the JUnit version in
the project >= 4.7 and the parallel attribute has ANY value use junit47 provider if JUnit >= 4.0 is present use junit4 provider else use junit3.8.1

2.1.2.外掛手動匹配

當然,如果你明確用的是JUnit4.7及以上版本,可以明確宣告:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
            <version>2.19</version>
        </dependency>
    </dependencies>
</plugin>

JUnit4.0(含)到JUnit4.7(不含)的版本,這樣宣告:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit4</artifactId>
            <version>2.19</version>
        </dependency>
    </dependencies>
</plugin>

JUnit3.8(含)到JUnit4.0(不含)的版本,這樣宣告:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit3</artifactId>
            <version>2.19</version>
        </dependency>
    </dependencies>
</plugin>

JUnit3.8以下的版本surefire不支援。建議大家用最新的JUnit版本,目前是4.12.

<dependencies>
    [...]
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    [...]        
</dependencies>

本文的例子我們用的Junit4.12.

2.2.準備測試用例

我們現在準備兩個類,一個被測試的類,一個測試用例.目錄結構如下

現在我們準備一個簡單的類.

package com.qyf404.learn.maven;

public class App {
    publicintadd(int a, int b) {
        return a + b;
    }
    publicintsubtract(int a, int b) {
        return a - b;
    }
}

再建立一個測試用例.

package com.qyf404.learn.maven;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class AppTest {
    private App app;
    @Before
    publicvoidsetUp() {
        app = new App();
    }
    @Test
    publicvoidtestAdd() throws InterruptedException {
        int a = 1;
        int b = 2;
        int result = app.add(a, b);
        Assert.assertEquals(a + b, result);
    }
    @Test
    publicvoidtestSubtract() throws InterruptedException {
        int a = 1;
        int b = 2;
        int result = app.subtract(a, b);
        Assert.assertEquals(a - b, result);
    }
    @After
    publicvoidtearDown() throws Exception {
    }
}

2.3.用maven執行測試用例

用maven執行測試用例很簡單,直接執行mvn test就可以.一般我們執行maven打包命令mvn package前maven會預設執行test命令.

qyfmac$ mvn test
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building learn-maven 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ learn-maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/qyfmac/git/learn-maven/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ learn-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/qyfmac/git/learn-maven/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ learn-maven ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/qyfmac/git/learn-maven/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ learn-maven ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 2 source files to /Users/qyfmac/git/learn-maven/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.19:test (default-test) @ learn-maven ---
[WARNING] The parameter forkMode is deprecated since version 2.14. Use forkCount and reuseForks instead.
[INFO] Surefire report directory: /Users/qyfmac/git/learn-maven/target/surefire-reports
[INFO] Using configured provider org.apache.maven.surefire.junit4.JUnit4Provider

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.qyf404.learn.maven.AppTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec - in com.qyf404.learn.maven.AppTest

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.622 s
[INFO] Finished at: 2015-12-01T11:36:04+08:00
[INFO] Final Memory: 14M/228M
[INFO] ------------------------------------------------------------------------
qyfmac$ 

其實我們會發現關鍵內容還是JUnit的東西,maven只是作為一個呼叫器呼叫了一下這些測試用例.

3.進階

後面讓我們來研究一下maven-surefire-plugin這個外掛更多的知識,這些多數都是和配置相關的.

3.1.跳過測試用例

在工作中,很多情況下我們打包是不想執行測試用例的,可能是測試用例不完事,或是測試用例會影響資料庫資料.跳過測試用例執行過程有三個種方法.

3.1.1.在configuration中宣告

在外掛的configuration配置中宣告跳過測試用例

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire
            
           

相關推薦

MavenMaven Surefire Plugin(JUnit)

1.maven-surefire-plugin是個什麼鬼? 如果你執行過mvn test或者執行其他maven命令時跑了測試用例,你就已經用過maven-surefire-plugin了。maven-surefire-plugin是maven裡執

maven外掛mavenmaven-surefire-plugin

1.maven-surefire-plugin是個什麼鬼? 如果你執行過 mvn test 或者執行其他maven命令時跑了測試用例,你就已經用過 maven-surefire-plugin 了。 maven-surefire-plugin 是maven裡執行測試用例的

maven maven簡介及安裝

分享 運行 項目信息 ava 信息 專家 not 編碼 bin 1. maven:行內,專家。基於java的項目構建功能,依賴管理,項目信息管理,Maven是一個項目構建工具。 2.構建:指日常開發中經常完成的除了編碼外的一些動作。如:清理,編譯,測試,運行,報告,部署等動

mavenmaven項目構建演練

apm target dsr lis rim oct targe yuv 項目構建 %E4%B8%AA%E5%BF%99%E7%9D%80%E6%89%BE%E5%AE%9E%E4%B9%A0%E5%B7%A5%E4%BD%9C%E7%9A%84%E5%A4%A7%E4%B

MavenMaven私服(三)

目錄 一、私服搭建 在區域網內搭建的maven的遠端倉庫伺服器稱為私服。 1. 下載nexus Nexus是Maven倉庫管理器,可以搭建maven倉庫,同時提供倉庫管理功能,構件搜尋功能等。 下載連結:http://www.

Maven的報錯Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4

報錯原始碼 Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://maven.aliyun.com/nexus/content/groups/publ

SpringBoot基礎(十):maven-surefire-plugin外掛部署

1、私有映象倉庫搭建 【搭建私有倉庫】 #拉去私有進行倉庫 [[email protected] ~]#docker pull registry #建立私有倉庫目錄 [[email protected] ~]# mkdir /opt/registry #啟動私有映象倉庫

maven插件介紹maven-jar-plugin

content letter post end version class pid 技術交流 cut maven-jar-plugin 插件的maven依賴為: <dependency> <groupId>org.apache.maven

maven打包工程出現錯誤 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test

cut apach 解決辦法 blog 研究 gpo failed fire 描述 今天用maven在命令行打包項目的時候出現錯誤: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:

maven install時報錯 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test)

ive form ron dev isp nag about oal KS 今天在一個maven項目上執行maven install命令的時候一直報錯,錯誤信息如下: [INFO] -------------------------------------------

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test

今天用maven在命令列打包專案的時候出現錯誤: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project xxx: There are

maven install時報錯Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test

今天jetty執行時發生了下面這個錯誤 最後找到的解決辦法是 在maven專案中的pom.xml在新增 <plugin> <groupId>org.apache.maven.plugins</groupId> <a

Maven Build時提示:Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test

一、問題描述 Maven執行 mvn clean install 提示: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on pro

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on project sharp-common: Execution default-test of go

1 [INFO] Scanning for projects... 2 [INFO] 3 [INFO] -----------------------< com.sharp:sharp-common >----------------------- 4 [INFO] Buildin

maven-surefire-plugin外掛

轉自:https://sq.163yun.com/blog/article/173632756223238144 maven-surefire-plugin外掛 目前很多專案組的測試程式碼工程都是採用MAVEN+TESTNG的方式構造的。   因此測試程式碼project內的pom.xml就

Maven打包時跳過測試(maven-surefire-plugin

1、在pom檔案新增 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <

Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://repo1.mave

maven專案pom.xml報錯:Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://repo1.maven.org/maven2 w

大話專案管理工具Maven

前言 相信只要做過 Java 開發的童鞋們,對 Ant 想必都不陌生,我們往往使用 Ant 來構建專案,尤其是涉及到特別繁雜的工作量,一個 build.xml 能夠完成編譯、測試、打包、部署等很多工,這在很大的程度上解放了程式設計師們的雙手。

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test)

springboot打包出現錯誤 正文 在進行springboot打包時,打包不成功 錯誤描述 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22

最詳細的maven教程全攻略maven中級(二)

所有用Maven管理的真實的專案都應該是分模組的,每個模組都對應著一個pom.xml。它們之間通過繼承和聚合(也稱作多模組,multi-module)相互關聯。那麼,為什麼要這麼做呢?我們明明在開發一個專案,劃分模組後,匯入Eclipse變成了N個專案,這會帶來複雜度,給