【发布时间】:2014-04-15 20:06:30
【问题描述】:
我正在尝试为使用 CountDownLatch 的类编写一些 junit,并且我正在使用 jmockit 库进行 junit 测试。
public class MappedData {
private static final AtomicReference<Map<String, Map<Integer, String>>> mapped1 = new AtomicReference<Map<String, Map<Integer, String>>>();
private static final AtomicReference<Map<String, Map<Integer, String>>> mapped2 = new AtomicReference<Map<String, Map<Integer, String>>>();
private static final CountDownLatch firstSet = new CountDownLatch(1);
public static Map<String, Map<Integer, String>> getMapped1Table() {
try {
firstSet.await();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return mapped1.get();
}
public static Map<String, Map<Integer, String>> getMapped2Table() {
try {
firstSet.await();
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return mapped2.get();
}
}
在getMapped1Table 和getMapped2Table 方法中确保最简单的方法是什么 - 我能够抛出InterruptedException 以便我也可以涵盖该场景。如果你看看这两种方法,我有一个我无法覆盖的 catch 块。
MappedData.getMapped1Table()
有什么方法可以确保我上面的两种方法都在抛出InterruptedException?
更新:-
我想要做的是 - 当我进行 junit 测试时,如何让 firstSet.await() 抛出 InterruptedException。
【问题讨论】:
-
你正在抛出一个
IllegalStateException...你会为此进行测试。 -
我认为您要问的是如何让
firstSet.await()在测试时抛出InterruptedException。对吗? -
@MikeB:是的,这就是我需要的……抱歉,我不够清楚……
标签: java junit jmockit interrupted-exception