سؤال

I'm creating a daemon that watches a given directory for changes. When it detects changes it runs all the tests using JUnit that are present in the directory.

It does this by invoking

    private void runTestsInWatchedDirectory(){
    TestRunner runner;
    runner= new TestRunner(this.dir);
    org.junit.runner.JUnitCore.main(runner.getClass().getName());
}

with TestRunner being an small adaptation from this code: http://burtbeckwith.com/blog/?p=52.

It correctly finds and runs all tests, but when it is done, the daemon is terminated! I know JUnit kills child threads, but why does the daemon die? And how do I solve it?

هل كانت مفيدة؟

المحلول

From JUnitCore.java, you can see that JUnitCore#main calls System.exit():

public static void main(String... args) {
    Result result = new JUnitCore().runMain(new RealSystem(), args);
    System.exit(result.wasSuccessful() ? 0 : 1);
}

Use one of the other methods in the class, or copy the code from runMain:

Result runMain(JUnitSystem system, String... args) {
    system.out().println("JUnit version " + Version.id());

    JUnitCommandLineParseResult jUnitCommandLineParseResult = JUnitCommandLineParseResult.parse(args);

    RunListener listener = new TextListener(system);
    addListener(listener);

    return run(jUnitCommandLineParseResult.createRequest(defaultComputer()));
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top