Skip to content

Commit

Permalink
Merge pull request #61 from ocadotechnology/test-arranger-57
Browse files Browse the repository at this point in the history
test-arranger-57 Throw runtime exception when trying to override nonexistent fields
  • Loading branch information
mjureczko authored Jul 19, 2023
2 parents 93aff4c + 51aeee5 commit 2e9720e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ static <T> void applyOverrides(T result, Map<String, Supplier<?>> overrides) {
try {
Field field = result.getClass().getDeclaredField(key);
ReflectionUtils.setFieldValue(result, field, value.get());
} catch (NoSuchFieldException e) {
System.err.println("Field selected for an override i.e. " + key + " was not found in class " + type.getName());
} catch (IllegalArgumentException | IllegalAccessException e) {
System.err.println("Access rejected when trying to override " + key + " field in class " + type.getName() + ": " + e.getMessage());
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalArgumentException("Failed to override field " + key + " in class " + type.getName(), e);
}
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/ocadotechnology/gembus/test/testUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ import java.time.LocalDate
import java.util.function.Predicate
import java.util.function.Supplier

/**
* some<Type>{
* someTypeField = someValue
* }
*/
inline fun <reified T> some(adjustment: T.() -> Unit): T {
val result = some<T>()
result.adjustment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import static com.ocadotechnology.gembus.test.Arranger.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.ThrowableAssert.catchThrowable;

public class ArrangerTestOverrides {
HashMap<String, Supplier<?>> overrides = new HashMap<>();
Expand All @@ -42,20 +43,6 @@ void should_useValuesFromOverrides_when_anOverrideIsSuppliedForExistingField() {
assertThat(actual.number).isEqualTo(override);
}

@Test
void should_createInitializedObject_when_requestingOverrideOfNonExistingField() {
//given
overrides.put("nonexisting", () -> someInteger());

//when
ToOverride actual = some(ToOverride.class, overrides);

//then
assertThat(actual.text).isNotNull();
assertThat(actual.primitiveNumber).isNotEqualTo(0);
assertThat(actual.number).isNotEqualTo(0);
}

@Test
void should_overridePrimitives() {
//given
Expand Down Expand Up @@ -97,12 +84,24 @@ void should_createInitializedObject_when_supplyingWrongTypeInOverrides() {
overrides.put("text", () -> override);

//when
ToOverride actual = some(ToOverride.class, overrides);
Throwable actual = catchThrowable(() -> some(ToOverride.class, overrides));

//then
assertThat(actual.text).isNotNull();
assertThat(actual.primitiveNumber).isNotEqualTo(0);
assertThat(actual.number).isNotEqualTo(0);
assertThat(actual).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Can not set java.lang.String field com.ocadotechnology.gembus.test.ArrangerTestOverrides$ToOverride.text to java.lang.Long");
}

@Test
void should_throwException_when_supplyingOverrideForNonExistingField() {
//given
overrides.put("nonexisting", () -> someInteger());

//when
Throwable actual = catchThrowable(() -> some(ToOverride.class, overrides));

//then
assertThat(actual).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Failed to override field nonexisting in class com.ocadotechnology.gembus.test.ArrangerTestOverrides$ToOverride");
}

@Test
Expand Down

0 comments on commit 2e9720e

Please sign in to comment.