jar文件加载(来自JAVA)完成后启动测试体

问题描述 投票:0回答:1

我想用SOAP webservices编写UNIT测试。 Webservices在其他jar文件中工作,我尝试加载Runtime.getRuntime()。exec(// java-jar ...)。加载jar文件需要2分钟。当加载在新线程中时,测试在加载jar文件之前结束。如果加载在主线程中,则测试未完成。我试着用while循环监听HTTP响应,但是当循环工作时,jar文件没有加载。

@Before
    public void setUp() throws Exception {
        // Get path of jar file

        thread = new Thread() {
            public void run() {
                try {
                    Process process = Runtime.getRuntime().exec(path-to-java.exe -jar webservices.jar);

                    process.waitFor();

                    process.destroy();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        thread.start();

        int responseCode;
        do {
            responseCode = getResponseCodeHTTP("http://localhost:8080/services");
        } while (responseCode < 200 || responseCode >= 400);

        System.out.println("Web services have loaded");
    }

        public int getResponseCodeHTTP(String urlToRead) throws IOException {
        URL url = new URL(urlToRead);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        int result;
        try {
            conn.connect();
            result = conn.getResponseCode();
        } catch (ConnectException e){
            return 500;
        }
        return result;
    }
multithreading web-services unit-testing http soap
1个回答
0
投票

好。我想编写test,它将从jar文件启动webservices(加载过程需要1.5分钟),然后执行测试。要启动Web服务,我使用Runtime.getRuntime()。exec并了解它们已启动我使用HTTP响应代码。代码[200-400]时,表示ws启动正常。

我尝试在新线程中调试代码并使用InputStreamReader和循环添加代码。

String line;
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
while ((line = reader.readLine()) != null)
    System.out.println("[Stdout] " + line);

并且测试成功执行。当我用readline删除while循环时,问题重复了。我还不明白它为什么会起作用。

© www.soinside.com 2019 - 2024. All rights reserved.