我正在尝试测试零除的功能。
如果我只是做:System.out.println(5/0),我会得到java.lang.ArithmeticException
我正在尝试测试是否引发了异常。
这是我的单元测试:
@Test(expected = ArithmeticException.class)
public void given_divideByZeroInput_when_divideByZero_then_verify_throwing_exception() {
MathClass sut = new MathClass();
sut.div(10,0);
}
Run Code Online (Sandbox Code Playgroud)
我的div就是这么简单:
public float div(int x, int y) {
return (float) x/y;
}
Run Code Online (Sandbox Code Playgroud)
但是,单元测试失败,说明:
java.lang.AssertionError: Expected exception: java.lang.ArithmeticException
Run Code Online (Sandbox Code Playgroud)
我做错什么了?
我知道有很多方法可以测试抛出的异常,但是我试图坚持简单的方法 @Test(excepted = .. )
这是我的简单代码:
public class Main4 {
public static void main(String[] args) {
System.out.println("Hello from thread: "+Thread.currentThread().getName());
new Game().run();
System.out.println("I am dying ... ");
}
static class Game {
public void run() {
value();
}
private int value() {
int number = 0;
CompletionStage<Void> c = calculate().thenApply(i -> i + 3).thenAccept(i -> System.out.println("I am done, and my value is " + i));
return number;
}
private CompletionStage<Integer> calculate() {
CompletionStage<Integer> completionStage = new CompletableFuture<>();
Executors.newCachedThreadPool().submit(() -> {
System.out.println("I am in the thread: …Run Code Online (Sandbox Code Playgroud) java multithreading threadpool completable-future completion-stage