当使用Maven创建一个项目时,它会创建一个带main方法的类测试用例。
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>
我们可以添加更多的测试用例到测试目录。 首先我们添加两个静态方法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>
快捷键介绍Ctrl + Shift + Alt + V无格式黏贴 (必备)Ctrl + Shift + Alt + N前往指定的变量 / 方法Ctrl + Shift + Alt + S打开...
Win 快捷键Mac 快捷键介绍Ctrl + Shift + FCommand + Shift + F根据输入内容查找整个项目 或 指定目录内文件Ctrl + Shift + RCom...
F8 进入下一步,如果当前行断点是一个方法,则不进入当前方法体内F7 进入下一步,如果当前行断点是一个方法,则进入当前...
系统要求系统支持:只要是支持 GNOME 或 KDE 桌面系统,建议是 Ubuntu(32位和64位都可以)JDK 版本:Oracle JDK 1.6 或以上内存...
文件编码修改上图标注 1 所示,IDE 的编码默认是 UTF-8,Project Encoding 虽然默认是 GBK,但是一般我都建议修改为...
Tomcat 输出 war 包如上图 Gif 所示,除了在 Artifacts 中需要配置,还需要在容器中也跟着配置,这样在启动容器的时候才会输...
Debug 设置如上图标注 1 所示,表示设置 Debug 连接方式,默认是 Socket。Shared memory 是 Windows 特有的一个属性...
Eclipse 窗口说明Eclipse 工作台(Workbench)首先,让我们来看一下Eclipse 作台用户界面,和它里面的各种组件。工作台是多个窗口...