简体   繁体   中英

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. There are more then 800 Junit tests.

Now we need to move to Maven build structure. Problem is that maven only runs junit where the class name has naming convention 'Test'.

How do i run the junit test which are in in src/main/java/* ?

Also, Is there a way where i can pull all methods that has '@Test' annotations?

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. Now that you've moved to Maven, you must comply with its way of doing things and follow its conventions. 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 ).

  • Create a *Test class for each class that contains @Test annotated methods and place them under 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.

You have to change the configuration of the Surefire plugin , which runs the tests. 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? 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.

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. 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.

If you are using eclipse, you can still execute it as Junit test.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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