您的位置:58脚本 > maven 测试 Maven命令测试

maven 测试 Maven命令测试

2023-05-13 03:32 Maven教程

maven 测试 Maven命令测试

maven 测试 Maven命令测试

maven 测试

Maven教程 - Maven命令测试


当使用Maven创建一个项目时,它会创建一个带main方法的类测试用例。

null

AppTest.java的内容

package com.java2s.ide;

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


public class AppTest 
    extends TestCase
{
    
    public AppTest( String testName )
    {
        super( testName );
    }

    
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    
    public void testApp()
    {
        assertTrue( true );
    }
}

要通过Maven运行单元测试,请发出以下命令:

c:mvn_testxmlFileEditor>mvn test

上面的代码生成以下结果。

c:mvn_testxmlFileEditor>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_testxmlFileEditorsrcmainresources
[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_testxmlFileEditorsrctestresources
[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_testxmlFileEditortargetsurefire-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_testxmlFileEditor>


Maven教程 - Maven命令测试...

我们可以添加更多的测试用例到测试目录。 首先我们添加两个静态方法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_testxmlFileEditor>mvn test

上面的代码生成以下结果。

c:mvn_testxmlFileEditor>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_testxmlFileEditortarget
[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_testxmlFileEditorsrcmainresources
[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_testxmlFileEditortargetclasses
[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_testxmlFileEditorsrctestresources
[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_testxmlFileEditortargettest-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ xmlFileEditor ---
[INFO] Surefire report directory: c:mvn_testxmlFileEditortargetsurefire-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_testxmlFileEditor>

运行测试用例...

上面的命令运行所有测试用例

要运行单个测试( TestApp1 ),请发出以下命令:

mvn -Dtest=TestApp1 test

上述命令生成以下结果。

c:mvn_testxmlFileEditor>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_testxmlFileEditorsrcmainresources
[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_testxmlFileEditorsrctestresources
[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_testxmlFileEditortargetsurefire-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_testxmlFileEditor>

跳过测试

我们可以通过使用以下命令跳过测试。

mvn package -Dmaven.test.skip=true

上述命令生成以下结果。

c:mvn_testxmlFileEditor>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_testxmlFileEditorsrcmainresources
[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_testxmlFileEditor>
阅读全文
以上是58脚本为你收集整理的maven 测试 Maven命令测试全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 58脚本 58jiaoben.com 版权所有 联系我们
桂ICP备12005667号-28 Powered by CMS