Skip to content

Commit 79b87f0

Browse files
authored
Revert "Propagate common basetype for the extracting method (#3673)" (#3737)
This partially reverts 588d24b.
1 parent bf439b3 commit 79b87f0

3 files changed

Lines changed: 82 additions & 34 deletions

File tree

assertj-core/src/main/java/org/assertj/core/api/AbstractObjectAssert.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -872,18 +872,18 @@ public AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extracting(S
872872
*/
873873
@CheckReturnValue
874874
@SafeVarargs
875-
public final <T> AbstractListAssert<?, List<? extends T>, T, ObjectAssert<T>> extracting(Function<? super ACTUAL, ? extends T>... extractors) {
875+
public final AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extracting(Function<? super ACTUAL, ?>... extractors) {
876876
return extractingForProxy(extractors);
877877
}
878878

879879
// This method is protected in order to be proxied for SoftAssertions / Assumptions.
880880
// The public method for it (the one not ending with "ForProxy") is marked as final and annotated with @SafeVarargs
881881
// in order to avoid compiler warning in user code
882-
protected <T> AbstractListAssert<?, List<? extends T>, T, ObjectAssert<T>> extractingForProxy(Function<? super ACTUAL, ? extends T>[] extractors) {
882+
protected AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> extractingForProxy(Function<? super ACTUAL, ?>[] extractors) {
883883
requireNonNull(extractors, shouldNotBeNull("extractors")::create);
884-
List<T> values = Stream.of(extractors)
885-
.map(extractor -> extractor.apply(actual))
886-
.collect(toList());
884+
List<Object> values = Stream.of(extractors)
885+
.map(extractor -> extractor.apply(actual))
886+
.collect(toList());
887887
return newListAssertInstance(values).withAssertionState(myself);
888888
}
889889

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2025 the original author or authors.
12+
*/
13+
package org.assertj.tests.core.kotlin.api.object_
14+
15+
import org.assertj.core.api.Assertions.assertThatObject
16+
import org.junit.jupiter.api.Disabled
17+
import org.junit.jupiter.api.Test
18+
19+
class ObjectAssert_extracting_with_Function_Test {
20+
21+
@Test
22+
fun `should support lambda with implicit parameter`() {
23+
// WHEN/THEN
24+
assertThatObject(" ").extracting { it.trim() }.isEqualTo("")
25+
}
26+
27+
@Test
28+
fun `should support lambda with explicit parameter`() {
29+
// WHEN/THEN
30+
assertThatObject(" ").extracting { it -> it.trim() }.isEqualTo("")
31+
}
32+
33+
@Disabled("Does not compile with Kotlin < 2.x")
34+
@Test
35+
fun `should support lambda with implicit parameter on two chained calls`() {
36+
/*
37+
// WHEN/THEN
38+
assertThatObject(" ")
39+
.extracting { it.trim() }
40+
.isEqualTo("")
41+
.extracting { it.isEmpty() }
42+
.isEqualTo(true)
43+
*/
44+
}
45+
46+
@Test
47+
fun `should support method reference`() {
48+
// WHEN/THEN
49+
assertThatObject(" ").extracting(String::trim).isEqualTo("")
50+
}
51+
52+
@Disabled("Does not compile with Kotlin < 2.x")
53+
@Test
54+
fun `should support method reference on two chained calls`() {
55+
/*
56+
// WHEN/THEN
57+
assertThatObject(" ")
58+
.extracting(String::trim)
59+
.isEqualTo("")
60+
.extracting(String::isEmpty)
61+
.isEqualTo(true)
62+
*/
63+
}
64+
65+
}

assertj-tests/assertj-integration-tests/assertj-core-tests/src/test/java/org/assertj/tests/core/api/object/ObjectAssert_extracting_with_Function_Array_Test.java

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@
1717
import static org.assertj.core.api.BDDAssertions.then;
1818
import static org.assertj.core.error.ShouldNotBeNull.shouldNotBeNull;
1919
import static org.assertj.core.presentation.UnicodeRepresentation.UNICODE_REPRESENTATION;
20+
import static org.assertj.tests.core.testkit.AlwaysEqualComparator.ALWAYS_EQUALS;
21+
import static org.assertj.tests.core.testkit.AlwaysEqualComparator.ALWAYS_EQUALS_STRING;
2022

2123
import java.util.List;
2224
import java.util.function.Function;
25+
2326
import org.assertj.core.api.AbstractAssert;
2427
import org.assertj.core.api.AbstractListAssert;
2528
import org.assertj.core.api.ObjectAssert;
2629
import org.assertj.core.internal.Objects;
2730
import org.assertj.core.util.introspection.PropertyOrFieldSupport;
28-
import org.assertj.tests.core.testkit.AlwaysEqualComparator;
2931
import org.assertj.tests.core.testkit.Employee;
30-
import org.assertj.tests.core.testkit.Jedi;
3132
import org.assertj.tests.core.testkit.Name;
3233
import org.assertj.tests.core.testkit.NavigationMethodBaseTest;
33-
import org.assertj.tests.core.testkit.Person;
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
3636

3737
class ObjectAssert_extracting_with_Function_Array_Test implements NavigationMethodBaseTest<ObjectAssert<Employee>> {
3838

3939
private Employee luke;
4040

41-
private static final Function<Employee, String> firstName = employee -> employee.getName().getFirst();
41+
private static final Function<Employee, String> FIRST_NAME = employee -> employee.getName().getFirst();
4242

4343
@BeforeEach
4444
void setUp() {
@@ -50,35 +50,18 @@ void should_allow_extracting_values_using_multiple_lambda_extractors() {
5050
// GIVEN
5151
Function<Employee, Object> lastName = employee -> employee.getName().getLast();
5252
// WHEN
53-
AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> result = assertThat(luke).extracting(firstName, lastName);
53+
AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> result = assertThat(luke).extracting(FIRST_NAME, lastName);
5454
// THEN
5555
result.hasSize(2)
5656
.containsExactly("Luke", "Skywalker");
5757
}
5858

59-
@Test
60-
void should_allow_extracting_values_using_multiple_lambda_extractors_with_common_ancestor() {
61-
// GIVEN
62-
record Companions(Jedi jedi, Person person) {
63-
}
64-
Companions companions = new Companions(new Jedi("Luke", "blue"), new Person("Han"));
65-
Function<Companions, Jedi> jedi = Companions::jedi;
66-
Function<Companions, Person> person = Companions::person;
67-
// WHEN
68-
AbstractListAssert<?, List<? extends Person>, Person, ObjectAssert<Person>> result = assertThat(companions).extracting(jedi,
69-
person);
70-
// THEN
71-
result.hasSize(2)
72-
.allSatisfy(e -> assertThat(e.getName()).isNotBlank());
73-
}
74-
7559
@Test
7660
void should_allow_extracting_values_using_multiple_lambda_extractors_with_type() {
7761
// GIVEN
7862
Function<Employee, String> lastName = employee -> employee.getName().getLast();
7963
// WHEN
80-
AbstractListAssert<?, List<? extends String>, String, ObjectAssert<String>> result = assertThat(luke).extracting(firstName,
81-
lastName);
64+
AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> result = assertThat(luke).extracting(FIRST_NAME, lastName);
8265
// THEN
8366
result.hasSize(2)
8467
.containsExactly("Luke", "Skywalker");
@@ -101,7 +84,7 @@ void should_rethrow_any_extractor_function_exception() {
10184
throw explosion;
10285
};
10386
// WHEN
104-
Throwable error = catchThrowable(() -> assertThat(luke).extracting(firstName, bomb));
87+
Throwable error = catchThrowable(() -> assertThat(luke).extracting(FIRST_NAME, bomb));
10588
// THEN
10689
then(error).isSameAs(explosion);
10790
}
@@ -124,18 +107,18 @@ void extracting_should_keep_assertion_state() {
124107
ObjectAssert<Employee> assertion = assertThat(luke).as("test description")
125108
.withFailMessage("error message")
126109
.withRepresentation(UNICODE_REPRESENTATION)
127-
.usingComparator(AlwaysEqualComparator.ALWAYS_EQUALS)
128-
.usingComparatorForFields(AlwaysEqualComparator.ALWAYS_EQUALS_STRING,
110+
.usingComparator(ALWAYS_EQUALS)
111+
.usingComparatorForFields(ALWAYS_EQUALS_STRING,
129112
"foo")
130-
.usingComparatorForType(AlwaysEqualComparator.ALWAYS_EQUALS_STRING,
113+
.usingComparatorForType(ALWAYS_EQUALS_STRING,
131114
String.class);
132115
// WHEN
133-
AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> result = assertion.extracting(firstName, Employee::getName);
116+
AbstractListAssert<?, List<?>, Object, ObjectAssert<Object>> result = assertion.extracting(FIRST_NAME, Employee::getName);
134117
// THEN
135118
then(result.descriptionText()).isEqualTo("test description");
136119
then(result.info.overridingErrorMessage()).isEqualTo("error message");
137120
then(result.info.representation()).isEqualTo(UNICODE_REPRESENTATION);
138-
then(comparatorOf(result).getComparator()).isSameAs(AlwaysEqualComparator.ALWAYS_EQUALS);
121+
then(comparatorOf(result).getComparator()).isSameAs(ALWAYS_EQUALS);
139122
}
140123

141124
private static Objects comparatorOf(AbstractListAssert<?, ?, ?, ?> assertion) {

0 commit comments

Comments
 (0)