lamcpp
lamcpp

Reputation: 79

Run JunitCore.runClasses from Jenkins

I have two junit classes:

public class TestExample1 {

  @Test
  public void test() {
      System.out.println("running test1");
  }
}

and:

public class TestExample2 {

  @Test
  public void test() {
      System.out.println("running test2");
  }
}

I run this classes in Main:

public class Main{
    public static void main(String[] args) {
        result = JUnitCore.runClasses(TestExample1.class, TestExample2.class);
        for(Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
        System.out.println(result.wasSuccessful());

    }
}

How to run this code from Jenkins? Is there any possibility? For example using maven?

Upvotes: 0

Views: 75

Answers (1)

lunheur
lunheur

Reputation: 11

You can run both of those tests just by calling mvn test in your terminal (assuming Maven is all set up and you're in the top directory of the project). Maven will pick up any test classes that match this pattern:

  • **/Test*.java
  • **/*Test.java
  • **/*Tests.java
  • **/*TestCase.java

Then you don't need the Main class at all.

Check out this link for more information using JUnit 5 with Maven: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit-platform.html

And this one for JUnit 4 and earlier: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html

If you prefer to use the Main class, you can use it in Maven with mvn exec:java -Dexec.mainClass="com.example.Main".

Docs for mvn exec here: https://www.mojohaus.org/exec-maven-plugin/usage.html

Upvotes: 1

Related Questions