赞
踩
- dependencies {
- testImplementation(platform('org.junit:junit-bom:5.7.2'))
- testImplementation('org.junit.jupiter:junit-jupiter')
- }
-
- test {
- useJUnitPlatform()
- testLogging {
- events "passed", "skipped", "failed"
- }
- }
- import org.junit.jupiter.api.DisplayName;
- import org.junit.jupiter.api.Test;
-
- import java.time.Duration;
-
- import static org.junit.jupiter.api.Assertions.assertAll;
- import static org.junit.jupiter.api.Assertions.assertEquals;
- import static org.junit.jupiter.api.Assertions.assertThrows;
- import static org.junit.jupiter.api.Assertions.assertTimeout;
-
- @DisplayName("Junit5单元测试示例")
- public class Junit5Test {
-
- @Test
- @DisplayName("DisplayName注解测试")
- void testWithDisplayName() {
- }
-
- @Test
- @DisplayName("Assertions.assertEquals测试")
- void testAssertEquals(){
- String name = "hello junit5";
- assertEquals("hello junit5",name,"name not same");
- }
-
- @Test
- @DisplayName("Assertions.assertAll测试")
- void testAssertAll(){
- assertAll("判断是否全部相同",
- () -> assertEquals("hello", "hello"),
- () -> assertEquals("world", "world")
- );
- }
-
- @Test
- @DisplayName("Assertions.assertThrows测试")
- void exceptionTesting() {
- Exception exception = assertThrows(ArithmeticException.class, ()->{
- System.out.println(1/0);
- });
- assertEquals("/ by zero", exception.getMessage());
- }
-
- @Test
- @DisplayName("Assertions.assertTimeout测试")
- void timeoutExceeded() {
- assertTimeout(Duration.ofMillis(10), () -> {
- Thread.sleep(9);
- });
- }
-
- }
https://junit.org/junit5/docs/current/user-guide/
https://github.com/junit-team/junit5-samples/tree/r5.7.2/junit5-jupiter-starter-gradle
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。