Maven学习三 使用junit测试maven project

                     每个开发人员都会对自己的代码进行自定义的测试,可以是把项目run起来,手动点点页面按钮,看看操作场景和步骤点是否符合业务需要,是否存在UE上的问题。也有自己写几个测试类,把service类的输入输出是否符合标准都测试一番,这两大类,其实就是包括了前后端的测试工作,分工各有不同。在maven中集成了junit测试包,应该说maven可以集成任何你想的到或者想不到工具插件。

     在maven project-01项目中引入junit插件并不困难,只需要在pom.xml中配置好依赖包即可(IDE环境下,依赖项会自动生成)。剩下的测试代码的编写并没有任何不同。另外,test类文件需要按照/src/main/java/的文件结构(main改为test)。

    test文件夹结构如下图:


     还需要修改pom.xml文件,增加dependencies项,改后的pom.xml内容如下:
[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  4.    <modelVersion>4.0.0</modelVersion>  
  5.   
  6.    <groupId>jizg.study.maven.hello</groupId>  
  7.    <artifactId>hello-first</artifactId>  
  8.    <version>0.0.1-SNAPSHOT</version>  
  9.      
  10.    <dependencies>  
  11.           <dependency>  
  12.                <groupId>junit</groupId>  
  13.                <artifactId>junit</artifactId>  
  14.                <version>4.10</version>  
  15.                <scope>test</scope>  
  16.           </dependency>  
  17.    </dependencies>  
  18. </project>  


   接下来在/src/test/java/jizg/study/maven/hello 文件夹下创建TestHello.java文件,这里需要注意,test的包结构可以自定义,要注意必备的路径为/src/test/java/,TestHello.java内容如下:
  1. package jizg.study.maven.hello;  
  2.   
  3. import org.junit.*;  
  4. import static junit.framework.Assert.*;  
  5. import jizg.study.maven.hello.*;  
  6.   
  7. public class TestHello{  
  8.   
  9.      @Test  
  10.      public void testHello(){       
  11.           Hello h = new Hello();  
  12.           assertEquals(h.sayHello("jizg"),"hello :jizg");  
  13.      }  
  14.        
  15. }  


   最后,改好pom.xml和test类文件之后,可以输入mvn test命令,这会重新把项目build出来,并且输出TestHello.java中的test信息。

控制台输出如下:
  1. D:\study\maven\01>mvn test  
  2. [INFO] Scanning for projects...  
  3. [INFO]  
  4. [INFO] ------------------------------------------------------------------------  
  5. [INFO] Building hello-first 0.0.1-SNAPSHOT  
  6. [INFO] ------------------------------------------------------------------------  
  7. [INFO]  
  8. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ hello-firs  
  9. t ---  
  10. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e  
  11. . build is platform dependent!  
  12. [INFO] skip non existing resourceDirectory D:\study\maven\01\src\main\resources  
  13. [INFO]  
  14. [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ hello-first -  
  15. --  
  16. [INFO] Nothing to compile - all classes are up to date  
  17. [INFO]  
  18. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ he  
  19. llo-first ---  
  20. [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e  
  21. . build is platform dependent!  
  22. [INFO] skip non existing resourceDirectory D:\study\maven\01\src\test\resources  
  23. [INFO]  
  24. [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ hello  
  25. -first ---  
  26. [WARNING] File encoding has not been set, using platform encoding GBK, i.e. buil  
  27. d is platform dependent!  
  28. [INFO] Compiling 1 source file to D:\study\maven\01\target\test-classes  
  29. [INFO]  
  30. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ hello-first ---  
  31. [INFO] Surefire report directory: D:\study\maven\01\target\surefire-reports  
  32.   
  33. -------------------------------------------------------  
  34. T E S T S  
  35. -------------------------------------------------------  
  36. Running test.TestHello.TestHello  
  37. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.151 sec  
  38.   
  39. Results :  
  40.   
  41. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  
  42.   
  43. [INFO] ------------------------------------------------------------------------  
  44. [INFO] BUILD SUCCESS  
  45. [INFO] ------------------------------------------------------------------------  
  46. [INFO] Total time: 5.346s  
  47. [INFO] Finished at: Wed Oct 02 18:23:23 CST 2013  
  48. [INFO] Final Memory: 9M/22M  
  49. [INFO] ------------------------------------------------------------------------  
  50. D:\study\maven\01>  


     细心的朋友还会发现,在project-01目录下,新生成了一个target(项目输出)文件夹,下面包括surefire-reports(测试结果)和编译过后的class文件。mvn test可以很好的支持单元测试,maven下的好多命令可以完成其中奇葩怪异的任务,并且mvn 命令支持串行执行。比如,mvn  install、mvn clean build等等。
           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/qq_43685201/article/details/87705983
今日推荐