1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.junit.bct;
18
19 import static org.apache.juneau.junit.bct.BctUtils.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import org.apache.juneau.*;
23 import org.junit.jupiter.api.*;
24
25
26
27
28 class PropertyNotFoundException_Test extends TestBase {
29
30 @Nested
31 class A_construction extends TestBase {
32
33 @Test
34 void a01_messageConstructor() {
35 String message = "Custom error message";
36 var ex = new PropertyNotFoundException(message);
37
38 assertEquals(message, ex.getMessage());
39 assertNull(ex.getCause());
40 }
41
42 @Test
43 void a02_messageAndCauseConstructor() {
44 String message = "Custom error message";
45 var cause = new RuntimeException("Root cause");
46 var ex = new PropertyNotFoundException(message, cause);
47
48 assertEquals(message, ex.getMessage());
49 assertEquals(cause, ex.getCause());
50 }
51
52 @Test
53 void a03_propertyAndTypeConstructor() {
54 String propertyName = "invalidProperty";
55 Class<?> objectType = String.class;
56 var ex = new PropertyNotFoundException(propertyName, objectType);
57
58 assertEquals("Property 'invalidProperty' not found on object of type String", ex.getMessage());
59 assertNull(ex.getCause());
60 }
61
62 @Test
63 void a04_propertyTypeAndCauseConstructor() {
64 String propertyName = "missingField";
65 Class<?> objectType = Integer.class;
66 var cause = new RuntimeException("Field not found");
67 var ex = new PropertyNotFoundException(propertyName, objectType, cause);
68
69 assertEquals("Property 'missingField' not found on object of type Integer", ex.getMessage());
70 assertEquals(cause, ex.getCause());
71 }
72 }
73
74 @Nested
75 class B_integration extends TestBase {
76
77 @Test
78 void b01_thrownByPropertyExtractor() {
79 BeanConverter converter = BasicBeanConverter.DEFAULT;
80 var bean = new TestBean("test", 42);
81
82
83 PropertyNotFoundException ex = assertThrows(PropertyNotFoundException.class, () -> {
84 converter.getNested(bean, tokenize("nonExistentProperty").get(0));
85 });
86
87 assertTrue(ex.getMessage().contains("nonExistentProperty"));
88 assertTrue(ex.getMessage().contains("TestBean"));
89 }
90
91 @SuppressWarnings("cast")
92 @Test
93 void b02_exceptionHierarchy() {
94 var ex = new PropertyNotFoundException("test");
95
96
97 assertTrue(ex instanceof RuntimeException);
98 assertTrue(ex instanceof Exception);
99 assertTrue(ex instanceof Throwable);
100 }
101 }
102
103
104 static class TestBean {
105 final String name;
106 final int value;
107
108 TestBean(String name, int value) {
109 this.name = name;
110 this.value = value;
111 }
112
113 public String getName() { return name; }
114 public int getValue() { return value; }
115 }
116 }