Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I export JUnit test cases into an executable .jar?

I am using Selenium and JUnit to automate some testing. I want to be able to export this into a runnable jar file. I was not able to and I am assuming it is because there is no main method, JVM doesn't know what to run....

I saw this post how to export (JUnit) test suite as executable jar and it suggested adding a main and running the JUnit from there. (I would have commented on that post, but it didn't let me 😢)

public static void main(String[] args) {
    JUnitCore.main("folder.package.testClass");
}

I made a new java class with this main method in it but the test class is never executed (from eclipse). Also if I try to export the project, I get errors and it doesn't export.

For my purposed of exporting all the JUnit classes into 1 Jar file and running from there, what the best way I could do this? I might event build a small menu where the user can pick which test class they want to run (which I'm assuming will require a main method some where....).

like image 945
Alex Avatar asked Oct 19 '25 02:10

Alex


2 Answers

You can execute test by doing following(just checked current scenario):

  1. Create new Eclipse Java project
  2. Add junit and hamcrest jars to your build path(I'm using junit-4.12 and hamcrest-core-1.3). It should look:

enter image description here

  1. Create your test under test folder:

    package com.example.junit5;
    
    import static org.junit.Assert.assertTrue;
    
    import org.junit.Test;
    
    public class FirstTest {
    
        @Test
        public void testTrue() {
            System.out.println("Executing testTrue()");
            assertTrue(true);     
         }
    }
    
  2. Create your main(executor) class under src folder:

    package com.example.junit5;
    
    import org.junit.runner.JUnitCore;
    
    public class Executor {
    
        public static void main(String[] args) {
            JUnitCore.main("com.example.junit5.FirstTest");
        }
    
    }
    
  3. Execute your test as Java Application. Result should be:

enter image description here

My environment config is:

java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

Hope this will help

like image 87
gokareless Avatar answered Oct 21 '25 15:10

gokareless


The following code ended up working for me.

package myPackageName;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class testRunner {
    public static void main(String[] args) {
        Result result = JUnitCore.runClasses(AllTests.class);
        for (Failure failure : result.getFailures()) {
            System.out.println(failure.toString());
        }
    }
}
like image 33
Alex Avatar answered Oct 21 '25 16:10

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!