Maven命令測試

2018-01-09 19:18 更新

Maven教程 - Maven命令測試


當使用Maven創(chuàng)建一個項目時,它會創(chuàng)建一個帶main方法的類測試用例。

null

AppTest.java的內(nèi)容

package com.java2s.ide;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

要通過Maven運行單元測試,請發(fā)出以下命令:

c:\mvn_test\xmlFileEditor>mvn test

上面的代碼生成以下結(jié)果。

c:\mvn_test\xmlFileEditor>mvn test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xmlFileEditor 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xmlFileEditor ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ xmlFileEditor ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ xmlFileEditor ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ xmlFileEditor ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ xmlFileEditor ---
[INFO] Surefire report directory: c:\mvn_test\xmlFileEditor\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.java2s.ide.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.765 s
[INFO] Finished at: 2014-11-22T10:14:39-08:00
[INFO] Final Memory: 19M/369M
[INFO] ------------------------------------------------------------------------
c:\mvn_test\xmlFileEditor>


Maven教程 - Maven命令測試...

我們可以添加更多的測試用例到測試目錄。 首先我們添加兩個靜態(tài)方法App.java。 兩個dummy方法只是返回String常量。 我們要用這些方法來說明如何向Maven項目添加測試用例。

package com.java2s.ide;
public class App {
  public static void main(String[] args) {
     System.out.println(getHelloWorld());
  }
  public static String getHelloWorld() {
    return "Hello World";
  }
  public static String getHelloWorld2() {
    return "Hello World 2";
  }
}

我們可以通過向 test 文件夾中添加一個新類來對getHelloWorld()方法進行單元測試。

package com.java2s.ide;
import junit.framework.Assert;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestApp1 {
 
  public void testPrintHelloWorld() {
    Assert.assertEquals(App.getHelloWorld(), "Hello World");
  }
}

以下代碼顯示如何為getHelloWorld2()方法添加另一個單元測試。

package com.java2s.ide; 
import junit.framework.Assert;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class TestApp2 {

  public void testPrintHelloWorld2() {
    Assert.assertEquals(App.getHelloWorld2(), "Hello World 2");
  }
}




運行測試用例

添加這兩個測試用例后,我們可以再次運行下面的Maven命令用于測試。

c:\mvn_test\xmlFileEditor>mvn test

上面的代碼生成以下結(jié)果。

c:\mvn_test\xmlFileEditor>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xmlFileEditor 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ xmlFileEditor ---
[INFO] Deleting c:\mvn_test\xmlFileEditor\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xmlFileEditor ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ xmlFileEditor ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to c:\mvn_test\xmlFileEditor\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ xmlFileEditor ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ xmlFileEditor ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 3 source files to c:\mvn_test\xmlFileEditor\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ xmlFileEditor ---
[INFO] Surefire report directory: c:\mvn_test\xmlFileEditor\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.java2s.ide.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
Running com.java2s.ide.TestApp1
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec
Running com.java2s.ide.TestApp2
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.125 s
[INFO] Finished at: 2014-11-22T10:22:06-08:00
[INFO] Final Memory: 25M/369M
[INFO] ------------------------------------------------------------------------
c:\mvn_test\xmlFileEditor>

運行測試用例...

上面的命令運行所有測試用例

要運行單個測試( TestApp1 ),請發(fā)出以下命令:

mvn -Dtest=TestApp1 test

上述命令生成以下結(jié)果。

c:\mvn_test\xmlFileEditor>mvn -Dtest=TestApp1 test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xmlFileEditor 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xmlFileEditor ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ xmlFileEditor ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ xmlFileEditor ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ xmlFileEditor ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ xmlFileEditor ---
[INFO] Surefire report directory: c:\mvn_test\xmlFileEditor\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.java2s.ide.TestApp1
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.704 s
[INFO] Finished at: 2014-11-22T10:22:54-08:00
[INFO] Final Memory: 19M/369M
[INFO] ------------------------------------------------------------------------
c:\mvn_test\xmlFileEditor>

跳過測試

我們可以通過使用以下命令跳過測試。

mvn package -Dmaven.test.skip=true

上述命令生成以下結(jié)果。

c:\mvn_test\xmlFileEditor>mvn package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building xmlFileEditor 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ xmlFileEditor ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory c:\mvn_test\xmlFileEditor\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ xmlFileEditor ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ xmlFileEditor ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ xmlFileEditor ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ xmlFileEditor ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ xmlFileEditor ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.703 s
[INFO] Finished at: 2014-11-22T10:25:31-08:00
[INFO] Final Memory: 15M/369M
[INFO] ------------------------------------------------------------------------
c:\mvn_test\xmlFileEditor>
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號