简体   繁体   中英

How can my Maven project run both JUnit and TestNG without TestNG trying to run JUnit tests?

I have a Maven project in which I am required to execute both JUnit and TestNG tests. My pom.xml has setup the Maven Surefire plugin as follows:

<!-- ... -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <configuration>
    <argLine>-Xmx1024m</argLine>
    <threadCount>1</threadCount>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-testng</artifactId>
      <version>2.19.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.surefire</groupId>
      <artifactId>surefire-junit47</artifactId>
      <version>2.19.1</version>
    </dependency>
  </dependencies>
</plugin>
<!-- ... -->

Unfortunately, when I execute my unit tests using Surefire, all tests are correctly executed, but TestNG appears to be trying to execute the JUnit tests a second time, and an exception like the following is appearing in the logs.

org.testng.TestNGException: 
Failure in JUnit mode for class com.hotwire.api.pricing.impl.PricingConfigIT: could not create/run JUnit test suite: 
Cannot find JUnit method class junit.framework.TestSuite$1.warning
at org.testng.junit.JUnitTestRunner.runFailed(JUnitTestRunner.java:239)
at org.testng.junit.JUnitTestRunner.start(JUnitTestRunner.java:232)
at org.testng.junit.JUnitTestRunner.run(JUnitTestRunner.java:213)
at org.testng.TestRunner$1.run(TestRunner.java:672)
at org.testng.TestRunner.runWorkers(TestRunner.java:1003)
at org.testng.TestRunner.privateRunJUnit(TestRunner.java:703)
at org.testng.TestRunner.run(TestRunner.java:610)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1170)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1095)
at org.testng.TestNG.run(TestNG.java:1007)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:132)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:198)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:94)
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:147)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:290)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:242)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:121)

How can I prevent this exception from polluting my logs?

I thought that I would share my answer because it took a little while to figure out and my particular problem is somewhat different than the problems of others who have encountered this particular stack trace.

The solution is to add the following XML element to Surefire's configuration in the pom.xml file.

<configuration>
  <!-- pre-existing config -->
  <properties>
    <property>
      <name>junit</name>
      <value>false</value>
    </property>
  </properties>
</configuration>

This is documented further in the Surefire documentation on running JUnit and TestNG tests together .

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