Maven命令测试
2018-01-09 19:18 更新
Maven教程 - Maven命令测试
当使用Maven创建一个项目时,它会创建一个带main方法的类测试用例。
AppTest.java的内容
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运行单元测试,请发出以下命令:
c:\mvn_test\xmlFileEditor>mvn test
上面的代码生成以下结果。
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命令测试...
我们可以添加更多的测试用例到测试目录。 首先我们添加两个静态方法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
上面的代码生成以下结果。
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
),请发出以下命令:
mvn -Dtest=TestApp1 test
上述命令生成以下结果。
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
上述命令生成以下结果。
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>
以上内容是否对您有帮助:
← Maven命令
更多建议: