Skip to content

Commit 07140ed

Browse files
committed
Remove deprecated method calls in assertions
1 parent 7afa97b commit 07140ed

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
/**
3+
* Verifies that the actual ${class_to_assert}'s ${property} is equal to the given one.
4+
* @param ${property_safe} the given ${property} to compare the actual ${class_to_assert}'s ${property} to.
5+
* @return this assertion object.
6+
* @throws AssertionError - if the actual ${class_to_assert}'s ${property} is not equal to the given one.${throws_javadoc}
7+
*/
8+
public ${self_type} has${Property}(${propertyType} ${property_safe}) ${throws}{
9+
// check that actual ${class_to_assert} we want to make assertions on is not null.
10+
isNotNull();
11+
12+
// overrides the default error message with a more explicit one
13+
String assertjErrorMessage = "\nExpecting ${property} of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>";
14+
15+
// null safe check
16+
${propertyType} actual${Property} = actual.get${Property}();
17+
if (!Objects.deepEquals(actual${Property}, ${property_safe})) {
18+
failWithMessage(assertjErrorMessage, actual, ${property_safe}, actual${Property});
19+
}
20+
21+
// return the current assertion for method chaining
22+
return ${myself};
23+
}
24+
25+
/**
26+
* Verifies that the actual ${class_to_assert}'s ${property} is close to the given value by less than the given offset.
27+
* <p>
28+
* If difference is equal to the offset value, assertion is considered successful.
29+
* @param ${property_safe} the value to compare the actual ${class_to_assert}'s ${property} to.
30+
* @param offset the given offset.
31+
* @return this assertion object.
32+
* @throws AssertionError - if the actual ${class_to_assert}'s ${property} is not close enough to the given value.${throws_javadoc}
33+
*/
34+
public ${self_type} has${Property}CloseTo(${propertyType} ${property_safe}, ${propertyType} offset) ${throws}{
35+
// check that actual ${class_to_assert} we want to make assertions on is not null.
36+
isNotNull();
37+
38+
${propertyType} actual${Property} = actual.get${Property}();
39+
40+
// overrides the default error message with a more explicit one
41+
String assertjErrorMessage = String.format("\nExpecting ${property}:\n <%s>\nto be close to:\n <%s>\nby less than <%s> but difference was <%s>",
42+
actual${Property}, ${property_safe}, offset, Math.abs(${property_safe} - actual${Property}));
43+
44+
// check
45+
Assertions.assertThat(actual${Property}).overridingErrorMessage(assertjErrorMessage).isCloseTo(${property_safe}, Assertions.within(offset));
46+
47+
// return the current assertion for method chaining
48+
return ${myself};
49+
}

0 commit comments

Comments
 (0)