简体   繁体   English

如果测试不在测试目录中但在主目录中并且类名没有。* Test。*之类的关键字,如何在Maven中运行JUnit Test

[英]How to run JUnit Test in maven if the test are not in test dir but in main dir and the classname are not having keywords like .*Test.*

Here is the back ground. 这是背景。

I was working on a project which was using ant build. 我正在使用蚂蚁构建的项目。 When we use to create a new Class, we use to write the junit test in same class as ant supported it.The Class Name doesn't have 'Test' naming convention. 当我们用来创建一个新的Class时,我们会在与ant支持的类相同的类中编写junit测试。ClassName没有'Test'命名约定。 There are more then 800 Junit tests. 超过800个Junit测试。

Now we need to move to Maven build structure. 现在,我们需要转到Maven构建结构。 Problem is that maven only runs junit where the class name has naming convention 'Test'. 问题是,maven仅运行类名具有命名约定“ Test”的junit。

How do i run the junit test which are in in src/main/java/* ? 如何运行src / main / java / *中的junit测试?

Also, Is there a way where i can pull all methods that has '@Test' annotations? 另外,有没有办法可以拉出所有带有“ @Test”注释的方法?

Please let me know if you need any further info. 如果您需要更多信息,请告诉我。

Just because you used to do it like that with Ant, doesn't make it right to keep using it now. 仅仅因为您曾经使用Ant来做过那样的事情,并不适合现在就继续使用它。 Now that you've moved to Maven, you must comply with its way of doing things and follow its conventions. 现在,您已经迁移到Maven,您必须遵循其做事方式并遵守其约定。 One of them is to keep your production code separate from your tests. 其中之一是使您的生产代码与测试分开。 A mixture does not make sense, as you are in fact littering your code with useless (for clients of your code) methods. 混合是没有意义的,因为实际上您是在用无用的(对于代码的客户而言)方法乱扔代码。 While you can keep doing this and find workarounds, this is not the high-standard route to choose. 尽管您可以继续这样做并找到解决方法,但这不是选择的高标准方法。

What you really need to do as a next step is schedule some refactoring time and carry out the following tasks: 下一步真正需要做的是安排一些重构时间并执行以下任务:

  • Create src/test/java (and respectively -- src/test/resources ). 创建src/test/java (和分别创建src/test/java src/test/resources )。

  • Create a *Test class for each class that contains @Test annotated methods and place them under src/test/java . 为每个包含@Test注释方法的类创建一个*Test类,并将它们放在src/test/java

  • Move those methods to the respective new classes. 将这些方法移至相应的新类。

  • Move all your resources that are only used by tests to your src/test/resources directory. 将所有仅由测试使用的资源移至src/test/resources目录。

You have to change the configuration of the Surefire plugin , which runs the tests. 您必须更改运行测试的Surefire插件的配置。 I have not tested it, but you can try this configuration: 我没有测试过,但是您可以尝试以下配置:

<build>
  <testSourceDirectory>src/main/java</testSourceDirectory>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.16</version>
      <configuration>
        <includes>
          <include>**/*.java</include>
        </includes>
      </configuration>
    </plugin>
  </plugins>
</build>

I assume since you are using ant, you have a test suite? 我想既然您正在使用ant,那么您有一个测试套件吗? If so, the easiest way is to change the configuration to use the given test suite: 如果是这样,最简单的方法是更改​​配置以使用给定的测试套件:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.12.4</version>
  <configuration>
    <includes>
      <include>AllTest.class</include>
    </includes>
  </configuration>
  </plugin>

However, I'm not sure if it will look in the src folder too so you may have to move the test suite to the test directory. 但是,我不确定它是否也会出现在src文件夹中,因此您可能必须将测试套件移至测试目录。

As for pulling all methods that have the @Test annotation (I assume without a suite?) you will have to do that yourself if you aren't following the sure-fire test class naming conventions. 至于拉出所有带有@Test批注的方法(我假设没有套件?),如果您不遵循肯定测试类的命名约定,则必须自己做。 It isn't too hard to make your own test runner implementation that searches through all your classes in your class path or a sub-folder and finds all classes that meet your criteria and pass that to Junit to run. 制作自己的测试运行器实现并不难,它可以在类路径或子文件夹中搜索所有类,然后找到符合条件的所有类并将其传递给Junit来运行。

If you are using eclipse, you can still execute it as Junit test. 如果您使用的是eclipse,则仍可以将其作为Junit测试执行。

In fact, if your "test classes" didn't put in the test folder, it means that you do not intend to run them as "test" at the maven build action. 实际上,如果您的“测试类”没有放在test文件夹中,则意味着您不打算在maven build操作中将它们作为“测试”运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM