我前面说过,现在Java语言项目中流行的构建工具主要是ant和maven, 现在来看,maven更火和采用率更多,所以我不介绍ant和junit的集成,这篇来学习maven和junit的集成过程。
1.先把被测项目和junit测项目都转成maven项目
目前我们有这两个Java 工程,现在需要把这两个java工程都转换成maven工程。
右键ProteinTracker这个工程,选择configure->convert to maven project
这个界面什么都不填,直接默认点击Finish,在ProteinTrackerTest工程也需要重复上面步骤。
2.添加项目之间依赖
转成maven项目之后,会自动创建pom.xml,Eclipse已经自动帮你打开两个pom.xml
这里点击Junit这个测试项目的pom.xml,然后点击Dependencies,把ProteinTracker添加到当前这个测试项目的依赖中。
ProteinTracker
ProteinTracker
0.0.1-SNAPSHOT
把以上信息添加到
点击Add..
点击OK,接下来再添加一个junit依赖,有时候LATEST版本不识别,就需要写一个具体junit版本号。
点击OK
点击保存,依赖添加完成。
3.maven运行Junit用例
3.1 被测项目需要执行 maven install
选择被测项目(上面截图工程中第一个项目),右键-run as -maven install, 控制台会出现以下信息。
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< ProteinTracker:ProteinTracker >--------------------
[INFO] Building ProteinTracker 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ProteinTracker ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\yuangliu\eclipse-workspace2\ProteinTracker\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ ProteinTracker ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 3 source files to C:\Users\yuangliu\eclipse-workspace2\ProteinTracker\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ProteinTracker ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\yuangliu\eclipse-workspace2\ProteinTracker\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ ProteinTracker ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ ProteinTracker ---
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ ProteinTracker ---
[INFO] Building jar: C:\Users\yuangliu\eclipse-workspace2\ProteinTracker\target\ProteinTracker-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ ProteinTracker ---
[INFO] Installing C:\Users\yuangliu\eclipse-workspace2\ProteinTracker\target\ProteinTracker-0.0.1-SNAPSHOT.jar to C:\Users\yuangliu\.m2\repository\ProteinTracker\ProteinTracker\0.0.1-SNAPSHOT\ProteinTracker-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\yuangliu\eclipse-workspace2\ProteinTracker\pom.xml to C:\Users\yuangliu\.m2\repository\ProteinTracker\ProteinTracker\0.0.1-SNAPSHOT\ProteinTracker-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.572 s
[INFO] Finished at: 2019-07-08T16:42:05+08:00
[INFO] ------------------------------------------------------------------------
3.2 测试项目,run as maven test
先编辑测试项目的pom.xml,添加一行代码
4.0.0
ProteinTrackerTest
ProteinTrackerTest
0.0.1-SNAPSHOT
src
src
maven-compiler-plugin
3.8.0
1.8
ProteinTracker
ProteinTracker
0.0.1-SNAPSHOT
junit
junit
4.13-beta-3
保存之后,右键Junit测试项目,或者pom.xml文件,选择run sa -> maven test
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< ProteinTrackerTest:ProteinTrackerTest >----------------
[INFO] Building ProteinTrackerTest 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ProteinTrackerTest ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ ProteinTrackerTest ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 5 source files to C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ProteinTrackerTest ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ ProteinTrackerTest ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 5 source files to C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ ProteinTrackerTest ---
[INFO] Surefire report directory: C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running test.HelloJunitTest
Run Hello Junit
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.1 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.434 s
[INFO] Finished at: 2019-07-08T16:55:38+08:00
[INFO] ------------------------------------------------------------------------
这样在执行maven test的时候,就会在build之后执行测试用例,这里有一个规则,你的测试类名称必须test结尾。我们这里来测试以下把test.TheoryTests.java改成test.TheoryTest.java
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< ProteinTrackerTest:ProteinTrackerTest >----------------
[INFO] Building ProteinTrackerTest 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ ProteinTrackerTest ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ ProteinTrackerTest ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 5 source files to C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ ProteinTrackerTest ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ ProteinTrackerTest ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 5 source files to C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ ProteinTrackerTest ---
[INFO] Surefire report directory: C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\target\surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running test.HelloJunitTest
Run Hello Junit
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.108 sec
Running test.TheoryTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec <<< FAILURE!
testThory(test.TheoryTest) Time elapsed: 0.031 sec <<< FAILURE!
org.junit.experimental.theories.internal.ParameterizedAssertionError: testThory("-4" )
at org.junit.experimental.theories.Theories$TheoryAnchor.reportParameterizedError(Theories.java:293)
at org.junit.experimental.theories.Theories$TheoryAnchor$1$1.evaluate(Theories.java:242)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithCompleteAssignment(Theories.java:223)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithAssignment(Theories.java:209)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithIncompleteAssignment(Theories.java:217)
at org.junit.experimental.theories.Theories$TheoryAnchor.runWithAssignment(Theories.java:207)
at org.junit.experimental.theories.Theories$TheoryAnchor.evaluate(Theories.java:192)
at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:365)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:330)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:78)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:328)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:65)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:292)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
at org.junit.runners.ParentRunner.run(ParentRunner.java:412)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.AssertionError
at org.junit.Assert.fail(Assert.java:87)
at org.junit.Assert.assertTrue(Assert.java:42)
at org.junit.Assert.assertTrue(Assert.java:53)
at test.TheoryTest.testThory(TheoryTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
at org.junit.experimental.theories.Theories$TheoryAnchor$2.evaluate(Theories.java:279)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:305)
at org.junit.experimental.theories.Theories$TheoryAnchor$1$1.evaluate(Theories.java:237)
... 28 more
Results :
Failed tests: testThory(test.TheoryTest): testThory("-4" )
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.773 s
[INFO] Finished at: 2019-07-08T16:58:42+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project ProteinTrackerTest: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Users\yuangliu\eclipse-workspace2\ProteinTrackerTest\target\surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException