gpt4 book ai didi

unit-testing - 如果在另一个线程中抛出异常,则单元测试失败

转载 作者:行者123 更新时间:2023-12-04 19:29:36 24 4
gpt4 key购买 nike

目前,每当我需要使测试失败以响应另一个线程中抛出的异常时,我会编写如下内容:

package com.example;

import java.util.ArrayList;
import java.util.List;
import org.testng.annotations.Test;

import static java.util.Arrays.asList;
import static java.util.Collections.synchronizedList;
import static org.testng.Assert.fail;

public final class T {
@Test
public void testFailureFromLambda() throws Throwable {
final List<Throwable> errors = synchronizedList(new ArrayList<>());

asList("0", "1", "2").parallelStream().forEach(s -> {
try {
/*
* The actual code under test here.
*/
throw new Exception("Error " + s);
} catch (final Throwable t) {
errors.add(t);
}
});

if (!errors.isEmpty()) {
errors.forEach(Throwable::printStackTrace);

final Throwable firstError = errors.iterator().next();
fail(firstError.getMessage(), firstError);
}
}
}

同步列表可以替换为 AtomicReference<Throwable> ,但总的来说,代码几乎保持不变。

是否有使用 Java 中可用的任何测试框架( TestNGJUnitHamcrestAssertJ 等)来执行相同操作的标准(且不那么冗长)方法吗?

最佳答案

默认情况下,TestNG 会在抛出异常时使测试方法失败。我相信 JUnit 也会发生同样的事情,其中​​它将测试标记为 errored , 如果它抛出一个意外的异常。

如果您要处理 Streams ,那么您需要将其包裹在 RuntimeException 中变体,因此 Java 不会提示。 TestNG 会自动使测试失败。

这是一个示例:

@Test
public void testFailureFromLambdaRefactored() {
asList("0", "1", "2").parallelStream().forEach(s -> {
try {
/*
* The actual code under test here.
*/
if (s.equals("2")) {
throw new Exception("Error " + s);
}
} catch (final Throwable t) {
throw new RuntimeException(t);
}
});
}

这适用于涉及 lambda 和流的场景。一般而言,如果您想了解从 @Test 分出的新线程中发生的异常。方法,那么你需要使用 ExecutorService .

这是一个示例:
@Test
public void testFailureInAnotherThread() throws InterruptedException, ExecutionException {
List<String> list = asList("0", "1", "2");
ExecutorService service = Executors.newFixedThreadPool(2);
List<Future<Void>> futures = service.invokeAll(Arrays.asList(new Worker(list)));
for (Future future : futures) {
future.get();
}

}

public static class Worker implements Callable<Void> {
private List<String> list;

public Worker(List<String> list) {
this.list = list;
}

@Override
public Void call() throws Exception {
for (String s : list) {
if (s.equals("2")) {
throw new Exception("Error " + s);
}
}
return null;
}
}

关于unit-testing - 如果在另一个线程中抛出异常,则单元测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45759903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com