jeudi 9 mai 2019

Java test file works when I run as Junit Test, not working as Java application

I have one java class file where I wrote my codes and one test file where I test my code.

I have generated my test cases via the Eclipse test generator and wrote my codes with @Test. When I run these codes with JUnit Test everything working, but I want to export as a jar and execute it. So, I need a main inside the test class. I searched google and StackOverflow

public static void main(String[] args) {
        JUnitCore jCore = new JUnitCore();
        jCore.run(StackTest.class);
        System.out.println("In main method");
    }

Another approach;

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

or

public static void main(String args[]) {

 TestRunner.runAndWait(new TestSuite(StackTest.class));

JUnitCore runner = new JUnitCore();
TextListener tl = new TextListener(new RealSystem());
runner.addListener(tl);
runner.run(StackTest.class);
// If above doesn't work, comment everything including imports and uncomment the following
org.junit.runner.JUnitCore.main("StackTest");
   }

Like these solutions, but nothing worked for me. How can I make executable tests for Java Application not run as JUnit? Because I want to export test in a jar and execute it outside.

My Errors usually like below:

JUnit version 4.12 .E Time: 0.005 There was 1 failure: 1) initializationError(StackTest) java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191) at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128) at org.junit.runners.ParentRunner.validate(ParentRunner.java:416) at org.junit.runners.ParentRunner.(ParentRunner.java:84) at org.junit.runners.BlockJUnit4ClassRunner.(BlockJUnit4ClassRunner.java:65) at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:10) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26) at org.junit.runner.Computer.getRunner(Computer.java:40) at org.junit.runner.Computer$1.runnerForClass(Computer.java:31) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101) at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87) at org.junit.runners.Suite.(Suite.java:81) at org.junit.runner.Computer.getSuite(Computer.java:28) at org.junit.runner.Request.classes(Request.java:75) at org.junit.runner.JUnitCommandLineParseResult.createRequest(JUnitCommandLineParseResult.java:118) at org.junit.runner.JUnitCore.runMain(JUnitCore.java:77) at org.junit.runner.JUnitCore.main(JUnitCore.java:36) at StackTest.main(StackTest.java:144)

FAILURES!!! Tests run: 1, Failures: 1

Aucun commentaire:

Enregistrer un commentaire