Java 8 AssertJ 的异常(Exception )断言
This commit is contained in:
parent
56c352b6f6
commit
550291e2e0
@ -7,8 +7,6 @@
|
|||||||
|
|
||||||
如果计算结果为 false ,则断言失败,抛出 AssertionError。
|
如果计算结果为 false ,则断言失败,抛出 AssertionError。
|
||||||
|
|
||||||
assertion(断言) 在软件开发中是一种常用的调试方式,很多开发语言中都支持这种机制。
|
|
||||||
|
|
||||||
### 相关文章
|
### 相关文章
|
||||||
|
|
||||||
- [AssertJ’s Java 8 Features](http://www.baeldung.com/assertJ-java-8-features)
|
- [AssertJ’s Java 8 Features](http://www.baeldung.com/assertJ-java-8-features)
|
||||||
|
@ -8,8 +8,15 @@ import static org.assertj.core.api.Assertions.catchThrowable;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* AssertJ Exception example
|
||||||
|
* <p><a href="https://www.ossez.com/t/assertj-exception/13988">https://www.ossez.com/t/assertj-exception/13988</a></p>
|
||||||
|
*
|
||||||
|
* @author YuCheng Hu
|
||||||
|
*/
|
||||||
public class Java8StyleAssertions {
|
public class Java8StyleAssertions {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -18,13 +25,11 @@ public class Java8StyleAssertions {
|
|||||||
ArrayList<String> myStringList = new ArrayList<String>(Arrays.asList("Strine one", "String two"));
|
ArrayList<String> myStringList = new ArrayList<String>(Arrays.asList("Strine one", "String two"));
|
||||||
myStringList.get(2);
|
myStringList.get(2);
|
||||||
}).isInstanceOf(IndexOutOfBoundsException.class)
|
}).isInstanceOf(IndexOutOfBoundsException.class)
|
||||||
.hasMessageStartingWith("Index: 2")
|
.hasMessageStartingWith("Index 2")
|
||||||
.hasMessageContaining("2")
|
.hasMessageContaining("2")
|
||||||
.hasMessageEndingWith("Size: 2")
|
.hasMessageEndingWith("length 2")
|
||||||
.hasMessageContaining("Index: 2, Size: 2")
|
.hasMessageContaining("Index 2")
|
||||||
.hasMessage("Index: %s, Size: %s", 2, 2)
|
.hasNoCause();
|
||||||
.hasMessageMatching("Index: \\d+, Size: \\d+")
|
|
||||||
.hasNoCause();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -36,18 +41,18 @@ public class Java8StyleAssertions {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}).isInstanceOf(RuntimeException.class)
|
}).isInstanceOf(RuntimeException.class)
|
||||||
.hasCauseInstanceOf(IOException.class)
|
.hasCauseInstanceOf(IOException.class)
|
||||||
.hasStackTraceContaining("IOException");
|
.hasStackTraceContaining("IOException");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenDividingByZero_thenArithmeticException() {
|
public void whenDividingByZero_thenArithmeticException() {
|
||||||
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
|
assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> {
|
||||||
int numerator = 10;
|
int numerator = 10;
|
||||||
int denominator = 0;
|
int denominator = 0;
|
||||||
int quotient = numerator / denominator;
|
int quotient = numerator / denominator;
|
||||||
})
|
})
|
||||||
.withMessageContaining("/ by zero");
|
.withMessageContaining("/ by zero");
|
||||||
|
|
||||||
// Alternatively:
|
// Alternatively:
|
||||||
|
|
||||||
@ -60,7 +65,6 @@ public class Java8StyleAssertions {
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
assertThat(thrown).isInstanceOf(ArithmeticException.class)
|
assertThat(thrown).isInstanceOf(ArithmeticException.class)
|
||||||
.hasMessageContaining("/ by zero");
|
.hasMessageContaining("/ by zero");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user