Changeset 18454 in josm for trunk/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java
- Timestamp:
- 2022-05-19T20:09:13+02:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/testutils/mockers/ExtendedDialogMocker.java
r17275 r18454 143 143 } 144 144 145 /** 146 * Get the result field for an extended dialog instance 147 * @param instance The instance to get the result field for 148 * @return The result field. May be private. 149 * @throws NoSuchFieldException If the field cannot be found. Should never be thrown. 150 */ 151 protected Field getResultField(ExtendedDialog instance) throws NoSuchFieldException { 152 // Note that subclasses of ExtendedDialogMocker will not have "result" as a declared field. 153 // Iterate up the chain until we get to a field that has "result" as a declared field. 154 // Only reason for this is just in case someone overrides the logic in ExtendedDialog. 155 Class<?> clazz = instance.getClass(); 156 Field resultField = null; 157 // Store the exception, if any 158 NoSuchFieldException noSuchFieldException = null; 159 while (!Object.class.equals(clazz) && resultField == null) { 160 try { 161 resultField = clazz.getDeclaredField("result"); 162 } catch (NoSuchFieldException e) { 163 clazz = instance.getClass().getSuperclass(); 164 // Only save the first exception 165 if (noSuchFieldException == null) { 166 noSuchFieldException = e; 167 } 168 } 169 } 170 if (resultField == null) { 171 throw noSuchFieldException; 172 } 173 return resultField; 174 } 175 145 176 @Mock 146 177 private void setupDialog(final Invocation invocation) { … … 160 191 final int mockResult = this.getMockResult(instance); 161 192 // TODO check validity of mockResult? 162 Field resultField = instance .getClass().getDeclaredField("result");193 final Field resultField = this.getResultField(instance); 163 194 resultField.setAccessible(true); 164 195 resultField.set(instance, mockResult);
Note:
See TracChangeset
for help on using the changeset viewer.