问题
There is a maven project hosted by someone else, which has src/main
directory and src/test
directory. The src/test
dir contains a java file ending with Test
, I'll just call it ATest.java
, and the class ATest
only contains a static void main function. It is like
//omitted
public class ATest {
public static void main(String[] args) throws Exception {
BTester.main(null);
CTester.main(null);
}
}
where BTester
and CTester
are both files under directory
src/main/java/<package_path>/tester
When I type mvn clean test
, it shows
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
I tried the following methods:
import org.junit.Test
and add@Test
descriptor ahead of the main function, but it gives error because main is static and it accepts parameters. Then I tried to moveBTester.main
andCTester.main
to another functiontestmain
and add@Test
beforetestmain
, but it causes errors when compiling, saying that I have uncaught exception, even if I addthrows Exception
?use the
maven-surefire-plugin
, here is how I did it in thepom.xml
file<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20</version> <configuration> <includes> <include>ATest.java</include> </includes> </configuration> </plugin>
But it still doesn't work.
What did I miss? I'm not very familiar with Java, not with maven, so I really need some help now. Thanks.
回答1:
- Try to place your test class under /src/test/java.
- You need your test method. Do not using main for test method using JUNIT See this reference: http://www.vogella.com/tutorials/JUnit/article.html
来源:https://stackoverflow.com/questions/43833127/no-test-run-when-running-maven-test-command