0

I would like to be able to execute tests I have written in java on another machine, which is not otherwise configured. Currently, I have to open up my IDE on my machine which has the Java SDK installed and maven set up, in order to run my tests. I want to allow other people, without a development environment and without any technical knowledge, to be able to run my as above and verify the results.

If possible it would be like this:

  • put an executable file on a computer that I otherwise have not touched,
  • run the executable,
  • view the test results.

How do I generate the simplest feasible standalone executable that runs the same selenium tests I can already run in my IDE?

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
user2180076
  • 121
  • 8

1 Answers1

1

This is completely possible, so long as the machine you run the tests on has java. Build your tests in the normal way, and create your executable jar with all dependencies included. Then you can deploy that jar on another machine and run it from there.

If you're using maven, then shade will build your all-dependencies-included jar file for you. You can also do it using assembly though it's a little harder.

The not-so-obvious part of the solution is to use the JUnit runner main class as your executable's main class, which you can set up according to Oracle's tutorial. Since you've got all your dependencies in your jar, you don't have to worry about the classpath. All you need to know is that the class to run is org.junit.runner.JUnitCore(see this answer), and it takes the names of the annotated test classes that you want to run. Once you've done all that, you can simply run java -jar YourJarFileOfTests.jar yourorg.yourpackage.YourJunitTestClass.

If you want to go one step further and remove the hassle of passing the test class names into java -jar, then you can create a simple suite that pulls in all your test classes for convenience. You can even create your own main class that delegates to JUnitCore with your hardcoded includes-all-tests suite. Once your done that, you can simply run java -jar YourJarFileOfTests.jar and it will call JUnitCore with your suite class as a parameter, and Robert is the brother of your mother.

Community
  • 1
  • 1
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
  • This sounds like exactly what I want to do, but Im a bit unclear on how to do it. I added the shade plugin with maven easily, but Im not sure how to use it. As for the first answer you gave regarding running tests from the command line, I am also a bit unsure how to use that- do I need to edit my code? And where do I put the path to my jar (using Junit 4.0). Thanks a lot for the help – user2180076 Jul 08 '14 at 01:36
  • Extended the answer a bit more. There are plenty of questions and answers about executable jars on SO; if my answer isn't clear enough, post a comment with a link to answers you find more useful and I'll update my answer with references to them. – Paul Hicks Jul 08 '14 at 03:53
  • The link I provided to Shade includes includes an [example on how to activate it in your pom](http://maven.apache.org/plugins/maven-shade-plugin/usage.html). You don't need to do anything else; the default operation of Shade is to always shade your jar. Have a look at how big your jar is after you've shaded it; it's hundreds of Ks bigger, I'll bet. – Paul Hicks Jul 08 '14 at 03:55
  • @user2180076 Have you managed to get it to work? Was there anything unclear in my answer that I can improve for future readers? – Paul Hicks Jul 09 '14 at 21:26