0

I am trying to export JUnit classes into 1 executable JAR. I can't do this because it doesn't have a main.

What I have tried:

I tried making a testingSuite but that did not work as well. I can run the JUnit class from Eclipse, I can also run the testingSuite - and it calls all JUnit classes I tell it too - they work fine in eclipse. Note, I had to go down to JUnit4 to use the testingSuite. Since I could not export the testingSuite either, I tried making a new class with one main method that calls the testingSuite, I cannot get this to run from Eclipse.

I have been going through Stack overflow and other sites for about 2 days, so now I will post =).

Anyone know how I can export multiple JUnit test classes into 1 executable Jar that can run all the classes when it is opened?

Yug Singh
  • 3,112
  • 5
  • 27
  • 52
Alex
  • 160
  • 1
  • 2
  • 22
  • I think you putting your energy in the wrong place. The proper way of testing a larger code base is to use a *build* system. And to have a **test** "target. "Go build system, **test** *all*". That is how everybody else does this. You are about to re-invent your own, much less powerful version of that wheel. Just don't. – GhostCat Aug 20 '18 at 19:14
  • I am using this for regression testing, with selenium. There is no tester, so I am building a basic testing suite that our BA (and sometimes me) can run when needed. (The testing we do currently is, the BA clicking through the website ) – Alex Aug 20 '18 at 19:22
  • Well, then you should look into putting up a [mcve] here. If you have "not working code", well, then share a minimal version of that with usl – GhostCat Aug 20 '18 at 19:27
  • @GhostCat FTLOA, why would you ask for that? We don't really need to see a bunch of JUnit test cases, along with the code required to make them pass. – Dawood ibn Kareem Aug 20 '18 at 19:30
  • http://benjamintan.io/blog/2014/10/14/running-junit-test-from-the-command-line/ – Dawood ibn Kareem Aug 20 '18 at 19:32
  • There may be something useful at https://stackoverflow.com/q/2235276 for you. – Dawood ibn Kareem Aug 20 '18 at 19:34
  • Code works, was just having issue exporting because there is nothing to define what runs... (no main...). I ended up getting it to work. I'll post answer below. – Alex Aug 21 '18 at 12:38

2 Answers2

0

If you're goal is to run some selenium tests and if your tests arn't too big, why not use selenium ide (firefox plugin, and here for chrome)?

It depends on if you want these tests to be maintainable and evolutive but if they're just there to check things still work, give it a try. Plus it will allow your BA to write their own tests. No need to know about programming, just click. Sort of.

This whole end to end test thing is very expensive to maintain but if your app doesn't evolves too much on the surface (its UI) then it might be worthy.

For an in depth article about testing in general, including testing pyramid, read this by Martin Fowler, it's very good.

will
  • 61
  • 1
  • 12
  • Yah, There won't be to many changes and if they are they will be far between. The BA knows enough of writing simple test cases. It won't be a complicated testing tool, just enough to make sure everything still works, an to add new tests when there is something new (rarely). – Alex Aug 21 '18 at 12:37
0

I was able to make it work by having a regular class call my Test Suite Class which calls my JUnit Test Classes. I don't know why it wasn't working before, but this time when I tried to export and there was a new option there.

Solution Below


JUnit test suite class (runs all the test classes I put into @SuiteClasses, called by 'TestRunner' class)

package myPackageName; 

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({ TestClass1.class, TestClass2.class })
public class AllTests {    
}

TestRunner Class, the class that is exported into the executable jar. This was the missing piece, without it, export would not work.

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());
        }
    }
}

Export Steps

  1. Click File, Click Export
  2. Open java Folder, Click 'Runnable JAR file', Click next
  3. Launch Configuration drop down shows an option 'myPackageName - TestRunner'. This is where I was able to pick the class that contains the main method that will be run by the JAR. (the issue I was having before, it wasn't there and if I selected other classes that appeared it gave an export error).
  4. I used the 'package required libraries into generated JAR' option for library handling, I think its correct because I have selenium libraries.
  5. Click Finish
  6. Run JAR by opening windows explorer and clicking it. Or, open CMD, cd to file directory, and run java -jar myJarName.jar.
Alex
  • 160
  • 1
  • 2
  • 22