在不同的项目上运行maven junit测试

时间:2017-08-10 19:03:18

标签: java multithreading maven runtime.exec maven-surefire-plugin

我需要使用maven surefire在项目上同时启动超时测试方法。现在,我使用下面的代码执行此操作:

Main - 创建一个由链接阻塞队列支持的新固定线程池。然后它提交runnables,使我们的maven cmd行调用操作系统。

public class Sandbox {

    public static void main(String[] args) {

        ExecutorService executor = new ThreadPoolExecutor(10, 10, 5, TimeUnit.MINUTES, new LinkedBlockingQueue<Runnable>());

        executor.submit(new RunCommandWithTimeout(10, TimeUnit.SECONDS, "cmd /C C:\\\\mavenTestCall1.bat"));
        executor.submit(new RunCommandWithTimeout(5, TimeUnit.MINUTES, "cmd /C C:\\\\mavenTestCall2.bat"));
    }
}

RunCommandWithTimeout - 此类调用RunTime.getRunTime()。exec传入包含maven调用的批处理文件。等待超时完成

public class RunCommandWithTimeout implements Runnable{
private Process process;
private int timeout;
private TimeUnit unit;
private String command;

public RunCommandWithTimeout(int timeout, TimeUnit unit, String command) {
    this.timeout = timeout;
    this.unit = unit;
    this.command = command;
}

@Override
public void run() {
    System.out.println(LocalTime.now() + " Starting call");
    try {
        Runtime runtime = Runtime.getRuntime();
        process = runtime.exec(command);

        if (!process.waitFor(timeout, unit)) {
            System.out.println(LocalTime.now() + " We timed out. Do stuff. Returning...");
            process.destroyForcibly();
            return;
        }


    } catch (InterruptedException ie) {
        System.out.println(LocalTime.now() + " There is an interrupted exception. Do stuff. Returning...");
        return;
    } catch (IOException e) {
        System.out.println(LocalTime.now() + " There is an io exception. Do stuff. Returning...");
        return;
    }

    System.out.println(LocalTime.now() + " Call complete"); 
}
}  

批处理文件示例

pushd \\NetworkDrive\MavenProjectDirectory
mvn test -e -Dtest=TestClass#TestMethod >> C:\PathToOutputLog

它主要起作用。测试同时进行并在他们应该的时候超时。

这种方法的问题是process.destroy不能阻止测试运行。我仍然可以报告说有一个超时并且测试导致失败花了太长时间,但这并没有阻止该过程完成对系统的测试。

有谁知道更好的方法?我知道有一个嵌入式版本的maven,但我找不到任何关于如何使用它的文档。

0 个答案:

没有答案
相关问题