2

I have 3 tests stored under a package that all take advantage of the JUnit @Test annotation. I do not have a main class in the project. Naturally, I can go Run As > Maven Test and all 3 tests will get executed.

I created a Jar executable file using the the Install command, but when I try to run the Jar file I get the message - "No main manifest attribute" message.

What is the solution for dealing with this in Maven?

I know that in Gradle, you do not need a main class to run the Jar executable.

Alternatively, would I need to create a main class containing a main method like this:

public static void main(String[] args){
List tests = new ArrayList();
tests.add(TestOne.class);
tests.add(TestTwo.class);

for (Class test : tests){
    runTests(test);
}
}

private static void runTests(Class test){
Result result = JUnitCore.runClasses(test);
for (Failure failure : result.getFailures()){
    System.out.println(failure.toString());
}
}

That kind of feels counter-intuitive to me however.

Nazrod12
  • 111
  • 1
  • 2
  • 9
  • I don't understand how you want to run a JAR with no main class? – Tunaki Jan 25 '17 at 10:46
  • I have 3 separate tests that all contain @Test annotations, but perhaps I do need a main class, if so how do I go about creating that? – Nazrod12 Jan 25 '17 at 10:46
  • 2
    But how do you intend to run it, at least conceptually? What exactly are you trying to achieve? – Tunaki Jan 25 '17 at 10:47
  • Well I would like it to execute the tests, just as it does when I run the Test command – Nazrod12 Jan 25 '17 at 10:49
  • Do you want to package your tests in a jar file? Check out this question: https://stackoverflow.com/questions/12828416/generate-test-jar-along-with-jar-file-in-test-package – default locale Jan 25 '17 at 10:54
  • What you're trying to achieve, and why, is really unclear. I can only guess you want to run tests from another JAR during the build of your project, so maybe look into [`dependenciesToScan`](http://maven.apache.org/components/surefire/maven-surefire-plugin/test-mojo.html#dependenciesToScan) of the Surefire plugin... – Tunaki Jan 25 '17 at 11:13
  • That looks useful, however when I run that it is still giving me the message "No main manifest attribute" so still scratching my head – Nazrod12 Jan 25 '17 at 11:15
  • Hi Tunaki, I am not sure what is unclear about it? So I created a Maven project that contains 3 JUnit tests in my automation framework that all use the @Test annotation. So I can run those tests using Run As > Maven Test. Now I would like to produce a build of the project and deploy as a Jar file, that when executed will of course execute those tests from the 3 JUnit tests. Does that make it clear? – Nazrod12 Jan 25 '17 at 11:33

1 Answers1

1

Generate test-jar

You could add this into your pom.xml to generate a jar with your all your tests:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <goals>
                <goal>test-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Execute test class

Then, execute the tests this way:

java -cp yourproject-1.0.0-tests.jar:junit-4.12.jar:otherdependencies.jar \ 
     junit.textui.TestRunner com.xyz.SomeTest

You need to add all your required dependencies in the -cp arguments.

Test Suite

Each test must be executed one by one, but you could create a testSuite, so you can easily execute all tests with a single command:

@RunWith(Suite.class)
@Suite.SuiteClasses({
        SomeTest.class,
        SomeOtherTest.class
})
public class SuiteAbcTest {
    //normally, this is an empty class
}

and execute the Test Suite:

java -cp yourproject-1.0.0-tests.jar:junit-4.12.jar:otherdependencies.jar \ 
     junit.textui.TestRunner com.xyz.SuiteAbcTest
alexbt
  • 16,415
  • 6
  • 78
  • 87
  • Excellent Alex, will give this a go and get back to you! – Nazrod12 Jan 25 '17 at 11:53
  • by the way, this assumes that your tests are in the maven's test folder: `src/test/java` (not in `src/main/java`) – alexbt Jan 25 '17 at 12:46
  • Ahh yes, I tried to compile using this but am getting some compilation error that says my package named test does not exist. Will try and resolve it. Will I still need a TestRunner class? – Nazrod12 Jan 25 '17 at 12:52
  • Alex, I think actually this assumes my tests are in src/main/java and not src/test/java? My tests are stored in src/test/java under a package called test, how do I reference those in the SuiteAbcTest class? – Nazrod12 Jan 25 '17 at 13:03
  • SuiteAbcTest must also be in `src/test/java`. Everything test related in the test folder. Then `maven-jar-plugin` will generate a jar with the tests classes, which you'll then be able to run with the TestRunner. – alexbt Jan 25 '17 at 13:04