1

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:

  1. 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 move BTester.main and CTester.main to another function testmain and add @Test before testmain, but it causes errors when compiling, saying that I have uncaught exception, even if I add throws Exception?

  2. use the maven-surefire-plugin, here is how I did it in the pom.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.

Tgn Yang
  • 330
  • 3
  • 16

1 Answers1

1
  1. Try to place your test class under /src/test/java.
  2. 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
hoang
  • 1,444
  • 11
  • 19
  • Thanks, it works now. Actually I have tried method 2 yesterday, but it just didn't work for some unknown resaons, and it suddenly works now... maybe I made some stupid mistakes. But anyway the problem is solved. – Tgn Yang May 08 '17 at 01:56
  • Whilst the answer and the OP code are correct, people should also not forget to end the name of their class "Test", So if I'm testing MyCode then MyCodeTest. Surefire by default looks for items ending in "Test". – PeterS May 23 '18 at 13:51