Skip to content

Commit 34398bd

Browse files
committed
feat:[Java 断言 Assert 使用教程与最佳实践](https://www.wdbyte.com/java/assert/)
1 parent a9d4766 commit 34398bd

10 files changed

Lines changed: 230 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969
- [Java 集合框架](https://www.wdbyte.com/java/collection/)
7070
- [Java 中使用 List ](https://www.wdbyte.com/java/list/)
7171

72+
### 代码测试
73+
- [Java 断言 Assert 使用教程与最佳实践](https://www.wdbyte.com/java/assert/)
74+
7275
## 😃Java I/O 教程
7376

7477
- [Java 创建和写入文件](https://www.wdbyte.com/java/io/file-create-write/)

core-java-modules/core-java-base/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@
2222
- [Java 枚举](https://www.wdbyte.com/java/enum/)
2323
- [Java 注释](*https://www.wdbyte.com/java/comment/*)
2424
- [Java 集合框架](https://www.wdbyte.com/java/collection/)
25-
- [Java 中使用 List ](https://www.wdbyte.com/java/list/)
25+
- [Java 中使用 List ](https://www.wdbyte.com/java/list/)
26+
- [Java 断言 Assert 使用教程与最佳实践](https://www.wdbyte.com/java/assert/)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.wdbyte.assert1;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* @author niulang
8+
* @date 2024/04/22
9+
*/
10+
public class AssertDemo1 {
11+
public static void main(String[] args) {
12+
13+
List<String> list = Arrays.asList("1", "2");
14+
boolean result = list.remove("x");
15+
//assert result;
16+
assert result : "移除失败";
17+
System.out.println(calc(100, 10));
18+
19+
// 手动开启断言
20+
//ClassLoader.getSystemClassLoader().setDefaultAssertionStatus(true);
21+
//System.out.println(calc(100, 0));
22+
}
23+
24+
public static int calc(int a, int b) {
25+
assert b != 0 : "除数不能为0";
26+
return a / b;
27+
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.wdbyte.assert1;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import static com.google.common.base.Verify.*;
7+
8+
/**
9+
* @author niulang
10+
* @date 2024/04/22
11+
*/
12+
public class AssertDemo2 {
13+
public static void main(String[] args) {
14+
int x = 100;
15+
verifyNotNull(x != 0);
16+
System.out.println(calc(100, 10));
17+
System.out.println(calc(100, 0));
18+
}
19+
20+
public static int calc(int a, int b) {
21+
verify(b != 0, "除数不能为0");
22+
return a / b;
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.wdbyte.assert1;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* @author niulang
8+
* @date 2024/04/22
9+
*/
10+
public class AssertDemo3 {
11+
static final boolean asserts = false; // 设置为 false 来消除断言
12+
13+
public static void main(String[] args) {
14+
List<String> list = Arrays.asList("1", "2");
15+
boolean result = list.remove("x");
16+
if (asserts) {
17+
assert result : "移除失败";
18+
}
19+
}
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.wdbyte.assert1;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
/**
7+
* @author niulang
8+
* @date 2024/04/22
9+
*/
10+
public class AssertDemo4 {
11+
12+
static {
13+
boolean assertsEnabled = false;
14+
assert assertsEnabled = true; // 故意产生副作用!!!
15+
if (!assertsEnabled) {
16+
throw new RuntimeException("必须启用断言!!!");
17+
}
18+
}
19+
20+
public static void main(String[] args) {
21+
List<String> list = Arrays.asList("1", "2");
22+
boolean result = list.remove("x");
23+
assert result : "移除失败";
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.wdbyte.assert1;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
import com.google.common.base.Preconditions;
7+
import com.google.common.base.Verify;
8+
import org.apache.commons.lang3.Validate;
9+
import org.junit.jupiter.api.Assertions;
10+
11+
/**
12+
* @author niulang
13+
* @date 2024/04/22
14+
*/
15+
public class AssertDemo5 {
16+
17+
public static void main(String[] args) {
18+
List<String> list = Arrays.asList("1", "2");
19+
boolean result = list.remove("x");
20+
Assertions.assertTrue(result);
21+
Preconditions.checkNotNull("","msg");
22+
Validate.isTrue(list.isEmpty(),"msg");
23+
Verify.verify(list.isEmpty(),"msg");
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.wdbyte.assert1;
2+
3+
public class InitializationDemo {
4+
5+
static {
6+
init();
7+
}
8+
9+
static void init() {
10+
System.out.println("Static initialization block called");
11+
// 假设这里有一个重要的初始化逻辑
12+
// 这个方法错误地在静态初始化之前被调用了
13+
assert isProperlyInitialized() : "System not properly initialized";
14+
}
15+
16+
static boolean isProperlyInitialized() {
17+
// 这里返回 false 模拟系统未被正确初始化
18+
return false;
19+
}
20+
21+
public InitializationDemo() {
22+
System.out.println("Constructor called");
23+
}
24+
25+
public static void main(String[] args) {
26+
new InitializationDemo();
27+
}
28+
}

core-java-modules/core-java-base/src/main/java/com/wdbyte/enum2/WeekdayTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public static void main(String[] args) {
1111
System.out.println("Today is Monday.");
1212
}
1313

14-
1514
Weekday[] weekdays = Weekday.values();
1615
for (Weekday weekday : weekdays) {
1716
System.out.println(weekday);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.wdbyte.thread;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.Future;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* @author niulang
10+
* @date 2023/11/01
11+
*/
12+
public class CompletableFutureTest {
13+
14+
/**
15+
* 异步执行程序后,对正常响应和异常响应进行处理
16+
*/
17+
@Test
18+
public void completableFutureTest1() {
19+
CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
20+
sleep(2000);
21+
System.out.println("do.....");
22+
return 1;
23+
});
24+
25+
completableFuture1.thenAccept(res -> {
26+
System.out.println("收到结果:" + res
27+
);
28+
});
29+
30+
System.out.println("等待");
31+
sleep(10 * 1000);
32+
}
33+
@Test
34+
public void completableFutureTest2() {
35+
CompletableFuture<Integer> completableFuture2 = CompletableFuture.supplyAsync(() -> {
36+
sleep(2000);
37+
System.out.println("do2.....");
38+
return 10 / 0;
39+
});
40+
completableFuture2.exceptionally(except -> {
41+
System.out.println("发生异常:" + except.getMessage());
42+
return 0;
43+
});
44+
45+
System.out.println("等待");
46+
sleep(10 * 1000);
47+
}
48+
49+
@Test
50+
public void completableFutureTest3() {
51+
CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
52+
sleep(2000);
53+
System.out.println("do.....");
54+
return 1;
55+
});
56+
57+
completableFuture1.thenAccept(res -> {
58+
System.out.println("收到结果:" + res
59+
);
60+
});
61+
62+
System.out.println("等待");
63+
sleep(10 * 1000);
64+
}
65+
66+
void sleep(long millis){
67+
try {
68+
Thread.sleep(millis);
69+
} catch (InterruptedException e) {
70+
throw new RuntimeException(e);
71+
}
72+
}
73+
74+
}

0 commit comments

Comments
 (0)