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