2

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

@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 didn't own in my code but there is no answer。

建议将不胜感激。

谢谢!

4

0 回答 0