how to export (JUnit) test suite as executable jar
Asked Answered
C

2

35

Is there a way in eclipse (Helios) to package/export my JUnit test suites (or maybe even test cases if possible) as executable jars?

I know how to generate runnable jars from projects with a main class, but i'm clueless about how to include a TestRunner.
Is there a straightforward way, or do I have to make a workaround main class calling the TestRunner somehow?

Details would be great.

Carrara answered 10/1, 2011 at 15:24 Comment(0)
K
42

You are correct that a main() method is needed for an executable jar.

It's easy to add a main method to your test suite though.

public static void main(String[] args) throws Exception {                    
       JUnitCore.main(
         "com.stackoverflow.MyTestSuite");            
}
Kutzenco answered 23/1, 2011 at 18:21 Comment(0)
S
1

The JUnit5 alternative to the JunitCore.main() approach from the accepted answer is ConsoleLauncher.main(). You can read more about ConsoleLauncher here, but TL;DR;

public static void main(String[] args) {                    
    ConsoleLauncher.main("execute", "--scan-class-path");
}

should be enough in most cases.

Spongy answered 2/4 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.