【问题标题】:I cannot run any java file that doesn't contain a specific string [closed]我无法运行任何不包含特定字符串的 java 文件 [关闭]
【发布时间】:2013-02-15 05:41:43
【问题描述】:

我看到一个奇怪的问题:我有大约 5 个用 java 编写的测试用例,我正在使用 maven 运行这些测试用例,但如果类名没有,我无法运行任何测试用例(.java 文件) t 包括关键字“测试”。

因此,我可以执行任何包含“Test”的类名(例如 LoginTest),但不能执行任何没有“Test”的类名(例如 LoginModule)。

【问题讨论】:

  • 你能显示一个你不能运行的类的代码吗?

标签: java maven pom.xml


【解决方案1】:

这不是奇怪的行为。这是一个 Maven 约定。通过以单词“Test”开始或结束类名来标识测试类。与任何 Maven 约定一样,您可以通过 supplying configuration for the surefire plugin 更改它,这是运行测试的内容。

【讨论】:

    【解决方案2】:

    谢谢大家,更新pom.xml文件后这个问题已经解决了:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
           <configuration>
              <includes>
                <include>**/*Page.java</include>
                <include>**/Page*.java</include>
                <include>**/*Test.java</include>
                <include>**/Test*.java</include>
             </includes>
          <redirectTestOutputToFile>true</redirectTestOutputToFile>
           </configuration>
    </plugin>
    

    【讨论】:

      【解决方案3】:

      重命名你的测试类可能是最简单的。

      否则,使用 surefire 插件,您可以指定要包含的测试用例。默认情况下,它具有以下规则:

      默认情况下,Surefire 插件会自动包含所有具有以下通配符模式的测试类:

      "**/Test*.java" - 包括其所有子目录和所有以“Test”开头的 java 文件名。 "**/*Test.java" - 包括其所有子目录和所有以“Test”结尾的 java 文件名。 "**/*TestCase.java" - 包括其所有子目录和所有以“TestCase”结尾的 java 文件名。

      如果您愿意,也可以包含特定的包:

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.13</version>
          <configuration>
            <includes>
              <include>**/package/*.java</include>
            </includes>
          </configuration>
        </plugin>
      

      http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-06-22
        • 2019-10-13
        • 2012-07-02
        • 1970-01-01
        • 2017-01-17
        • 2016-01-17
        • 2013-12-20
        相关资源
        最近更新 更多