0

I have a test project, to test a third party code. I have created a few JUnit tests for that, but since my tests are on src/main/java instead of src/tests, I don't think I can use surefire to export my test results.

I need the test results to be exported as xml, for jenkins to read them, but couldn't find a way to do that considering my tests are not on src/tests.

Could I still use surefire for that? Is there any other maven plugin to do that?

Alexander Rumanovsk
  • 2,043
  • 4
  • 21
  • 33

2 Answers2

0

You can use the testSourceDirectory tag of the Maven Surefire Plugin's configuration to override the default directory from where Test Classes are chosen.

Please refer below links for more information:

http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#testSourceDirectory

Maven project.build.testSourceDirectory property not working from profile

JSN
  • 2,035
  • 13
  • 27
  • You can also specify `src/main/java` as per https://maven.apache.org/guides/introduction/introduction-to-the-pom.html – SpaceTrucker Jun 29 '17 at 14:03
  • My problem is that even if I change the directory in the build to src/main/java, I would still have to use "mvn test" to run the tests. In my case, I produce an executable jar, and the tests are run using "java -jar" – Alexander Rumanovsk Jun 29 '17 at 14:28
  • If that's the case, why not add a main method to your JUnit Test Suite as explained in this post https://stackoverflow.com/questions/4648341/how-to-export-junit-test-suite-as-executable-jar ? – JSN Jun 29 '17 at 14:32
  • @Beginner I have a TestRunner class just like this one. I just need that when I run this main method, the test results to be exported to xml, just like surefire would do when I run "mvn test". This way, Jenkins can read them. – Alexander Rumanovsk Jun 29 '17 at 14:51
  • @AlexanderRumanovsk - See if this post helps https://stackoverflow.com/questions/9062412/generate-xml-files-used-by-junit-reports – JSN Jun 29 '17 at 15:00
0

For those who might be interested in this, I was able to export XML reports adding a listener to JUnitCore, following the example described here: https://ttddyy.github.io/generate-junit-xml-report-from-junitcore/

If you are interested on doing this, I suggest you take a look at cloudbee's code here: https://github.com/cloudbees/junit-standalone-runner

Here is my TestRunner code:

import java.io.File;
import java.io.FileOutputStream;

import org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter;
import org.junit.internal.TextListener;
import org.junit.runner.Computer;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;

import com.myproject.suites.MainTestSuite;
import com.myproject.util.JUnitResultFormatterAsRunListener;

public class TestRunner {

  public static void main(String[] args) {

    if (args.length <= 0) {
      throw new RuntimeException("Reports dir must be on arg[0]");
    }

    final File reportDir = new File(args[0]);

    JUnitCore junit = new JUnitCore();
    junit.addListener(new TextListener(System.out));
    if (reportDir != null) {
      reportDir.mkdirs();
      junit.addListener(new JUnitResultFormatterAsRunListener(new XMLJUnitResultFormatter()) {
        @Override
        public void testStarted(Description description) throws Exception {
          formatter.setOutput(new FileOutputStream(new File(reportDir, "TEST-" + description.getDisplayName()
            + ".xml")));
          super.testStarted(description);
        }
      });
    }
    junit.run(new Computer(), MainTestSuite.class).getFailureCount();

  }
}
Alexander Rumanovsk
  • 2,043
  • 4
  • 21
  • 33