diff --git a/src/main/java/org/assertj/core/api/AbstractObjectAssert.java b/src/main/java/org/assertj/core/api/AbstractObjectAssert.java index dbfa5f26f4..96c75e8135 100644 --- a/src/main/java/org/assertj/core/api/AbstractObjectAssert.java +++ b/src/main/java/org/assertj/core/api/AbstractObjectAssert.java @@ -654,7 +654,8 @@ public SELF hasFieldOrProperty(String name) { *
* Private fields are matched by default but this can be changed by calling {@link Assertions#setAllowExtractingPrivateFields(boolean) Assertions.setAllowExtractingPrivateFields(false)}. *
- * If you are looking to chain multiple assertions on different properties in a type safe way, consider chaining {@link #returns(Object, Function)} calls. + * If you are looking to chain multiple assertions on different properties in a type safe way, consider chaining + * {@link #returns(Object, Function)} and {@link #doesNotReturn(Object, Function)} calls. *
* Example: *
public class TolkienCharacter {
@@ -1093,6 +1094,32 @@ public SELF returns(T expected, Function from) {
return myself;
}
+ /**
+ * Verifies that the object under test does not return the given expected value from the given {@link Function},
+ * a typical usage is to pass a method reference to assert object's property.
+ *
+ * Wrapping the given {@link Function} with {@link Assertions#from(Function)} makes the assertion more readable.
+ *
+ * Example:
+ *
// from is not mandatory but it makes the assertions more readable
+ * assertThat(frodo).doesNotReturn("Bilbo", from(TolkienCharacter::getName))
+ * .doesNotReturn("Bilbo", TolkienCharacter::getName) // no from :(
+ * .doesNotReturn(null, from(TolkienCharacter::getRace));
+ *
+ * @param expected the value the object under test method's call should not return.
+ * @param from {@link Function} used to acquire the value to test from the object under test. Must not be {@code null}
+ * @param the expected value type the given {@code method} returns.
+ * @return {@code this} assertion object.
+ * @throws NullPointerException if given {@code from} function is null
+ *
+ * @since 3.22.0
+ */
+ public SELF doesNotReturn(T expected, Function from) {
+ requireNonNull(from, "The given getter method/Function must not be null");
+ objects.assertNotEqual(info, from.apply(actual), expected);
+ return myself;
+ }
+
/**
* Enable using a recursive field by field comparison strategy when calling the chained {@link RecursiveComparisonAssert#isEqualTo(Object) isEqualTo} assertion.
*
diff --git a/src/main/java/org/assertj/core/api/Assertions.java b/src/main/java/org/assertj/core/api/Assertions.java
index 4176086f28..0af4724ade 100644
--- a/src/main/java/org/assertj/core/api/Assertions.java
+++ b/src/main/java/org/assertj/core/api/Assertions.java
@@ -2211,13 +2211,14 @@ public static TemporalUnitOffset byLessThan(long value, TemporalUnit unit) {
}
/**
- * A syntax sugar to write fluent assertion using {@link ObjectAssert#returns(Object, Function)}.
+ * A syntax sugar to write fluent assertion using {@link ObjectAssert#returns(Object, Function)} and
+ * {@link ObjectAssert#doesNotReturn(Object, Function)}.
*
* Example:
*
Jedi yoda = new Jedi("Yoda", "Green");
* assertThat(yoda).returns("Yoda", from(Jedi::getName))
* .returns(2.4, from(Jedi::getHeight))
- * .returns(150, from(Jedi::getWeight));
+ * .doesNotReturn(null, from(Jedi::getWeight));
*
* @param extractor A function to extract test subject's property
* @param * Example: *
Jedi yoda = new Jedi("Yoda", "Green");
* assertThat(yoda).returns("Yoda", from(Jedi::getName))
* .returns(2.4, from(Jedi::getHeight))
- * .returns(150, from(Jedi::getWeight));
+ * .doesNotReturn(null, from(Jedi::getWeight));
*
* @param extractor A function to extract test subject's property
* @param * Example: *
Jedi yoda = new Jedi("Yoda", "Green");
* assertThat(yoda).returns("Yoda", from(Jedi::getName))
* .returns(2.4, from(Jedi::getHeight))
- * .returns(150, from(Jedi::getWeight));
+ * .doesNotReturn(null, from(Jedi::getWeight));
*
* @param extractor A function to extract test subject's property
* @param {@link AbstractObjectAssert#returns(Object, Function)}
.
- *
* @author Takuya "Mura-Mi" Murakami
*/
class ObjectAssert_returns_Test extends ObjectAssertBaseTest {
@@ -45,17 +40,20 @@ protected void verify_internal_effects() {
@Test
void should_fail_with_throwing_NullPointerException_if_method_is_null() {
- ThrowingCallable code = () -> assertions.returns("May the force be with you.", null);
- assertThatThrownBy(code).isExactlyInstanceOf(NullPointerException.class)
- .hasMessage("The given getter method/Function must not be null");
+ // WHEN
+ Throwable thrown = catchThrowable(() -> assertions.returns("May the force be with you.", null));
+ // THEN
+ then(thrown).isInstanceOf(NullPointerException.class)
+ .hasMessage("The given getter method/Function must not be null");
}
@Test
void perform_assertion_like_users() {
-
+ // GIVEN
Jedi yoda = new Jedi("Yoda", "Green");
+ // WHEN/THEN
assertThat(yoda).returns("Yoda", from(Jedi::getName))
.returns("Yoda", Jedi::getName);
-
}
+
}