TestNG以多线程方式生成断言(不在多线程中运行)

时间:2013-05-13 06:29:00

标签: java multithreading testng

我的问题是我需要以multiThread方式调用方法,例如

@Test
public void assignInMutiThread()
{
    Thread thread1 = new Thread(new AssignAndExpect(0));
    thread1.start();
    Thread thread2 = new Thread(new AssignAndExpect(1));
    thread2.start();
    thread1.join();
    thread2.join();
}

线程将发送一个http请求并检查响应是否等于期望。由于我需要覆盖public void run()方法,因此无法将响应代码返回给测试方法。

public class AssignExecuteAndAssert implements Runnable{

    int expectedAssert = 0;

    public AssignExecuteAndAssert(int expectedAssert)
    {
        this.expectedAssert = expectedAssert;
    }

    public void run() {
        HttpGet assign1 = new HttpGet("http://localhost:8707/assign?uuid="+(new Random().nextInt(99999)));
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(assign1);
        InputStream inputStream = response.getEntity().getContent();
        int resoponseStatus = inputStream.read();
        Assert.assertEquals(resoponseStatus, expectedAssert);
    }
}

问题是无论Assert是对还是错,这个测试方法总是被传递。 如果线程中的断言失败,有没有办法让测试失败?

我在这里找到类似的问题:Failing TestNG unit test from a callback I don't own in my code但没有答案。

建议将不胜感激。

谢谢!

0 个答案:

没有答案