Maven在JUnit5中不运行@ParameterizedTest。

huangapple go评论109阅读模式
英文:

Maven does not run @ParameterizedTest in JUnit5

问题

我在一个使用 @ParameterizedTest 注解的 Maven 项目中编写了以下测试代码:

  1. @ParameterizedTest
  2. @ValueSource(strings = {"", "test"})
  3. public void whenCreateCitation_thenCitationShouldNotBeNull(String s) {
  4. Citation citationLoaded = quotesLoaderBean.createCitation(s);
  5. assertNotNull("Citation should not be null", citationLoaded);
  6. }

测试结果显示,只有那些使用 @Test 注解的测试方法被执行了。我在 pom.xml 文件中尝试了一些更改,但并没有真正有效。以下是 pom 文件中相关的部分内容:

  1. <dependencies>
  2. <!-- 其他依赖项 -->
  3. <!-- ... -->
  4. <dependency>
  5. <groupId>org.junit.jupiter</groupId>
  6. <artifactId>junit-jupiter-engine</artifactId>
  7. <version>5.7.0</version>
  8. <scope>test</scope>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.junit.platform</groupId>
  12. <artifactId>junit-platform-runner</artifactId>
  13. <version>1.5.2</version>
  14. <scope>test</scope>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.junit.jupiter</groupId>
  18. <artifactId>junit-jupiter-params</artifactId>
  19. <version>5.7.0</version>
  20. <scope>test</scope>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.junit.jupiter</groupId>
  24. <artifactId>junit-jupiter-api</artifactId>
  25. <version>5.7.0</version>
  26. <scope>test</scope>
  27. </dependency>
  28. <!-- 其他依赖项 -->
  29. <!-- ... -->
  30. </dependencies>
  31. <build>
  32. <plugins>
  33. <plugin>
  34. <artifactId>maven-surefire-plugin</artifactId>
  35. <version>3.0.0-M5</version>
  36. </plugin>
  37. <plugin>
  38. <artifactId>maven-failsafe-plugin</artifactId>
  39. <version>3.0.0-M5</version>
  40. </plugin>
  41. </plugins>
  42. </build>

测试结果如下图所示:

Maven在JUnit5中不运行@ParameterizedTest。

从图中可以看出测试未被执行。

英文:

I wrote the following test in a Maven project which is annotated with @ParameterizedTest

  1. @ParameterizedTest
  2. @ValueSource(strings = {&quot;&quot;, &quot;test&quot;})
  3. public void whenCreateCitation_thenCitationShouldNotBeNull(String s) {
  4. Citation citationLoaded = quotesLoaderBean.createCitation(s);
  5. assertNotNull(&quot;Citation should not be null&quot;, citationLoaded);
  6. }

The test result shows that only the tests that are annotated with @Test where executed in this test class. I tried a few changes in the pom.xml but nothing really worked. Here is the relevant part of the the pom file:

  1. &lt;dependencies&gt;
  2. &lt;dependency&gt;
  3. &lt;groupId&gt;org.easymock&lt;/groupId&gt;
  4. &lt;artifactId&gt;easymock&lt;/artifactId&gt;
  5. &lt;version&gt;4.0.1&lt;/version&gt;
  6. &lt;scope&gt;test&lt;/scope&gt;
  7. &lt;type&gt;jar&lt;/type&gt;
  8. &lt;/dependency&gt;
  9. &lt;dependency&gt;
  10. &lt;groupId&gt;org.mockito&lt;/groupId&gt;
  11. &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
  12. &lt;version&gt;3.3.3&lt;/version&gt;
  13. &lt;scope&gt;test&lt;/scope&gt;
  14. &lt;/dependency&gt;
  15. &lt;dependency&gt;
  16. &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
  17. &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
  18. &lt;version&gt;5.7.0&lt;/version&gt;
  19. &lt;scope&gt;test&lt;/scope&gt;
  20. &lt;/dependency&gt;
  21. &lt;dependency&gt;
  22. &lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
  23. &lt;artifactId&gt;junit-platform-runner&lt;/artifactId&gt;
  24. &lt;version&gt;1.5.2&lt;/version&gt;
  25. &lt;scope&gt;test&lt;/scope&gt;
  26. &lt;/dependency&gt;
  27. &lt;dependency&gt;
  28. &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
  29. &lt;artifactId&gt;eclipselink&lt;/artifactId&gt;
  30. &lt;version&gt;2.5.2&lt;/version&gt;
  31. &lt;scope&gt;provided&lt;/scope&gt;
  32. &lt;/dependency&gt;
  33. &lt;dependency&gt;
  34. &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
  35. &lt;artifactId&gt;org.eclipse.persistence.jpa.modelgen.processor&lt;/artifactId&gt;
  36. &lt;version&gt;2.5.2&lt;/version&gt;
  37. &lt;scope&gt;provided&lt;/scope&gt;
  38. &lt;/dependency&gt;
  39. &lt;dependency&gt;
  40. &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
  41. &lt;artifactId&gt;gson&lt;/artifactId&gt;
  42. &lt;version&gt;2.8.2&lt;/version&gt;
  43. &lt;type&gt;jar&lt;/type&gt;
  44. &lt;/dependency&gt;
  45. &lt;dependency&gt;
  46. &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
  47. &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
  48. &lt;version&gt;1.3&lt;/version&gt;
  49. &lt;scope&gt;test&lt;/scope&gt;
  50. &lt;/dependency&gt;
  51. &lt;dependency&gt;
  52. &lt;groupId&gt;javax&lt;/groupId&gt;
  53. &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt;
  54. &lt;version&gt;7.0&lt;/version&gt;
  55. &lt;scope&gt;provided&lt;/scope&gt;
  56. &lt;/dependency&gt;
  57. &lt;dependency&gt;
  58. &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
  59. &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
  60. &lt;version&gt;${jersey.version}&lt;/version&gt;
  61. &lt;scope&gt;provided&lt;/scope&gt;
  62. &lt;/dependency&gt;
  63. &lt;!-- if you are using Jersey client specific features without the server side
  64. &lt;dependency&gt;
  65. &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
  66. &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
  67. &lt;version&gt;2.25.1&lt;/version&gt;
  68. &lt;scope&gt;provided&lt;/scope&gt;
  69. &lt;/dependency&gt;
  70. --&gt;
  71. &lt;dependency&gt;
  72. &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
  73. &lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
  74. &lt;version&gt;5.7.0&lt;/version&gt;
  75. &lt;scope&gt;test&lt;/scope&gt;
  76. &lt;/dependency&gt;
  77. &lt;dependency&gt;
  78. &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
  79. &lt;artifactId&gt;junit-jupiter-api&lt;/artifactId&gt;
  80. &lt;version&gt;5.7.0&lt;/version&gt;
  81. &lt;scope&gt;test&lt;/scope&gt;
  82. &lt;/dependency&gt;
  83. &lt;/dependencies&gt;
  84. &lt;build&gt;
  85. &lt;plugins&gt;
  86. &lt;plugin&gt;
  87. &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
  88. &lt;version&gt;3.0.0-M5&lt;/version&gt;
  89. &lt;/plugin&gt;
  90. &lt;plugin&gt;
  91. &lt;artifactId&gt;maven-failsafe-plugin&lt;/artifactId&gt;
  92. &lt;version&gt;3.0.0-M5&lt;/version&gt;
  93. &lt;/plugin&gt;

This is the test result:

Maven在JUnit5中不运行@ParameterizedTest。

As you can see the test is not executed.

答案1

得分: 0

使用以下的 pom.xml 配置,一切都正常运行。

  1. <properties>
  2. <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
  3. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  4. <jersey.version>2.25.1</jersey.version>
  5. <junit.jupiter.version>5.7.0</junit.jupiter.version>
  6. <junit.platform.version>1.7.0</junit.platform.version>
  7. </properties>
  8. <dependencies>
  9. <dependency>
  10. <groupId>org.junit.jupiter</groupId>
  11. <artifactId>junit-jupiter-engine</artifactId>
  12. <version>${junit.jupiter.version}</version>
  13. <scope>test</scope>
  14. </dependency>
  15. <!-- 其他依赖项... -->
  16. </dependencies>
  17. <build>
  18. <plugins>
  19. <plugin>
  20. <groupId>org.apache.maven.plugins</groupId>
  21. <artifactId>maven-compiler-plugin</artifactId>
  22. <version>3.1</version>
  23. <configuration>
  24. <source>1.7</source>
  25. <target>1.7</target>
  26. <compilerArguments>
  27. <endorseddirs>${endorsed.dir}</endorseddirs>
  28. </compilerArguments>
  29. </configuration>
  30. </plugin>
  31. <!-- 其他插件... -->
  32. </plugins>
  33. </build>

请注意,我在这里只翻译了 pom.xml 中的一部分示例内容,您可以根据需要继续翻译其他部分。

英文:

With the following pom.xml configuration everythings works just fine.

  1. &lt;properties&gt;
  2. &lt;endorsed.dir&gt;${project.build.directory}/endorsed&lt;/endorsed.dir&gt;
  3. &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  4. &lt;jersey.version&gt;2.25.1&lt;/jersey.version&gt;
  5. &lt;junit.jupiter.version&gt;5.7.0&lt;/junit.jupiter.version&gt;
  6. &lt;junit.platform.version&gt;1.7.0&lt;/junit.platform.version&gt;
  7. &lt;/properties&gt;
  8. &lt;dependencies&gt;
  9. &lt;dependency&gt;
  10. &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
  11. &lt;artifactId&gt;junit-jupiter-engine&lt;/artifactId&gt;
  12. &lt;version&gt;${junit.jupiter.version}&lt;/version&gt;
  13. &lt;scope&gt;test&lt;/scope&gt;
  14. &lt;/dependency&gt;
  15. &lt;dependency&gt;
  16. &lt;groupId&gt;org.junit.platform&lt;/groupId&gt;
  17. &lt;artifactId&gt;junit-platform-runner&lt;/artifactId&gt;
  18. &lt;version&gt;${junit.platform.version}&lt;/version&gt;
  19. &lt;scope&gt;test&lt;/scope&gt;
  20. &lt;/dependency&gt;
  21. &lt;dependency&gt;
  22. &lt;groupId&gt;org.junit.jupiter&lt;/groupId&gt;
  23. &lt;artifactId&gt;junit-jupiter-params&lt;/artifactId&gt;
  24. &lt;version&gt;5.7.0&lt;/version&gt;
  25. &lt;scope&gt;test&lt;/scope&gt;
  26. &lt;/dependency&gt;
  27. &lt;dependency&gt;
  28. &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
  29. &lt;artifactId&gt;eclipselink&lt;/artifactId&gt;
  30. &lt;version&gt;2.5.2&lt;/version&gt;
  31. &lt;scope&gt;provided&lt;/scope&gt;
  32. &lt;/dependency&gt;
  33. &lt;dependency&gt;
  34. &lt;groupId&gt;org.eclipse.persistence&lt;/groupId&gt;
  35. &lt;artifactId&gt;org.eclipse.persistence.jpa.modelgen.processor&lt;/artifactId&gt;
  36. &lt;version&gt;2.5.2&lt;/version&gt;
  37. &lt;scope&gt;provided&lt;/scope&gt;
  38. &lt;/dependency&gt;
  39. &lt;dependency&gt;
  40. &lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
  41. &lt;artifactId&gt;gson&lt;/artifactId&gt;
  42. &lt;version&gt;2.8.2&lt;/version&gt;
  43. &lt;type&gt;jar&lt;/type&gt;
  44. &lt;/dependency&gt;
  45. &lt;dependency&gt;
  46. &lt;groupId&gt;org.hamcrest&lt;/groupId&gt;
  47. &lt;artifactId&gt;hamcrest-core&lt;/artifactId&gt;
  48. &lt;version&gt;1.3&lt;/version&gt;
  49. &lt;scope&gt;test&lt;/scope&gt;
  50. &lt;/dependency&gt;
  51. &lt;dependency&gt;
  52. &lt;groupId&gt;javax&lt;/groupId&gt;
  53. &lt;artifactId&gt;javaee-web-api&lt;/artifactId&gt;
  54. &lt;version&gt;7.0&lt;/version&gt;
  55. &lt;scope&gt;provided&lt;/scope&gt;
  56. &lt;/dependency&gt;
  57. &lt;dependency&gt;
  58. &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
  59. &lt;artifactId&gt;jersey-container-servlet&lt;/artifactId&gt;
  60. &lt;version&gt;${jersey.version}&lt;/version&gt;
  61. &lt;scope&gt;provided&lt;/scope&gt;
  62. &lt;/dependency&gt;
  63. &lt;!-- if you are using Jersey client specific features without the server side
  64. &lt;dependency&gt;
  65. &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
  66. &lt;artifactId&gt;jersey-client&lt;/artifactId&gt;
  67. &lt;version&gt;2.25.1&lt;/version&gt;
  68. &lt;scope&gt;provided&lt;/scope&gt;
  69. &lt;/dependency&gt;
  70. --&gt;
  71. &lt;/dependencies&gt;
  72. &lt;build&gt;
  73. &lt;plugins&gt;
  74. &lt;plugin&gt;
  75. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  76. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  77. &lt;version&gt;3.1&lt;/version&gt;
  78. &lt;configuration&gt;
  79. &lt;source&gt;1.7&lt;/source&gt;
  80. &lt;target&gt;1.7&lt;/target&gt;
  81. &lt;compilerArguments&gt;
  82. &lt;endorseddirs&gt;${endorsed.dir}&lt;/endorseddirs&gt;
  83. &lt;/compilerArguments&gt;
  84. &lt;/configuration&gt;
  85. &lt;/plugin&gt;
  86. &lt;plugin&gt;
  87. &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
  88. &lt;version&gt;3.8.1&lt;/version&gt;
  89. &lt;/plugin&gt;
  90. &lt;plugin&gt;
  91. &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
  92. &lt;version&gt;2.22.0&lt;/version&gt;
  93. &lt;/plugin&gt;
  94. &lt;plugin&gt;
  95. &lt;artifactId&gt;maven-failsafe-plugin&lt;/artifactId&gt;
  96. &lt;version&gt;2.22.0&lt;/version&gt;
  97. &lt;/plugin&gt;
  98. &lt;plugin&gt;
  99. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  100. &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
  101. &lt;version&gt;2.3&lt;/version&gt;
  102. &lt;configuration&gt;
  103. &lt;failOnMissingWebXml&gt;false&lt;/failOnMissingWebXml&gt;
  104. &lt;/configuration&gt;
  105. &lt;/plugin&gt;
  106. &lt;plugin&gt;
  107. &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
  108. &lt;artifactId&gt;maven-dependency-plugin&lt;/artifactId&gt;
  109. &lt;version&gt;2.6&lt;/version&gt;
  110. &lt;executions&gt;
  111. &lt;execution&gt;
  112. &lt;phase&gt;validate&lt;/phase&gt;
  113. &lt;goals&gt;
  114. &lt;goal&gt;copy&lt;/goal&gt;
  115. &lt;/goals&gt;
  116. &lt;configuration&gt;
  117. &lt;outputDirectory&gt;${endorsed.dir}&lt;/outputDirectory&gt;
  118. &lt;silent&gt;true&lt;/silent&gt;
  119. &lt;artifactItems&gt;
  120. &lt;artifactItem&gt;
  121. &lt;groupId&gt;javax&lt;/groupId&gt;
  122. &lt;artifactId&gt;javaee-endorsed-api&lt;/artifactId&gt;
  123. &lt;version&gt;7.0&lt;/version&gt;
  124. &lt;type&gt;jar&lt;/type&gt;
  125. &lt;/artifactItem&gt;
  126. &lt;/artifactItems&gt;
  127. &lt;/configuration&gt;
  128. &lt;/execution&gt;
  129. &lt;/executions&gt;
  130. &lt;/plugin&gt;
  131. &lt;/plugins&gt;
  132. &lt;/build&gt;

答案2

得分: 0

请将 maven-surefire-plugin 升级到最新版本之一,例如:

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <artifactId>maven-surefire-plugin</artifactId>
  5. <version>3.0.0-M6</version>
  6. </plugin>
  7. </plugins>
  8. </build>
英文:

Please upgrade maven-surefire-plugin to one of the latest versions, e.g

  1. &lt;build&gt;
  2. &lt;plugins&gt;
  3. &lt;plugin&gt;
  4. &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
  5. &lt;version&gt;3.0.0-M6&lt;/version&gt;
  6. &lt;/plugin&gt;
  7. &lt;/plugins&gt;
  8. &lt;/build&gt;

huangapple
  • 本文由 发表于 2020年10月24日 18:57:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64512603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定