Skip to content

Replace AssertJ internal APIs with alternative implementations #46855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package org.springframework.boot.configurationprocessor.test;

import java.util.function.Function;

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AssertProvider;
import org.assertj.core.internal.Objects;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ObjectAssert;

import org.springframework.boot.configurationprocessor.metadata.ItemDeprecation;
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
Expand All @@ -28,34 +31,33 @@
* AssertJ assert for {@link ItemMetadata}.
*
* @author Stephane Nicoll
* @author Stefano Cordio
*/
public class ItemMetadataAssert extends AbstractAssert<ItemMetadataAssert, ItemMetadata>
implements AssertProvider<ItemMetadataAssert> {

private static final Objects objects = Objects.instance();

public ItemMetadataAssert(ItemMetadata itemMetadata) {
super(itemMetadata, ItemMetadataAssert.class);
objects.assertNotNull(this.info, itemMetadata);
isNotNull();
}

public ItemMetadataAssert isProperty() {
objects.assertEqual(this.info, this.actual.isOfItemType(ItemType.PROPERTY), true);
extracting((actual) -> actual.isOfItemType(ItemType.PROPERTY)).isEqualTo(true);
Copy link
Contributor Author

@scordio scordio Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I favored the usage of a custom extracting method to leverage the internal propagation of the assertion state.

In case that isn't a crucial feature for the current scope, such lines could also be written as:

Suggested change
extracting((actual) -> actual.isOfItemType(ItemType.PROPERTY)).isEqualTo(true);
Assertions.assertThat(this.actual.isOfItemType(ItemType.PROPERTY)).isTrue();

and the custom extracting method could be deleted.

return this;
}

public ItemMetadataAssert isGroup() {
objects.assertEqual(this.info, this.actual.isOfItemType(ItemType.GROUP), true);
extracting((actual) -> actual.isOfItemType(ItemType.GROUP)).isEqualTo(true);
return this;
}

public ItemMetadataAssert hasName(String name) {
objects.assertEqual(this.info, this.actual.getName(), name);
extracting(ItemMetadata::getName).isEqualTo(name);
return this;
}

public ItemMetadataAssert hasType(String type) {
objects.assertEqual(this.info, this.actual.getType(), type);
extracting(ItemMetadata::getType).isEqualTo(type);
return this;
}

Expand All @@ -64,7 +66,7 @@ public ItemMetadataAssert hasType(Class<?> type) {
}

public ItemMetadataAssert hasDescription(String description) {
objects.assertEqual(this.info, this.actual.getDescription(), description);
extracting(ItemMetadata::getDescription).isEqualTo(description);
return this;
}

Expand All @@ -73,7 +75,7 @@ public ItemMetadataAssert hasNoDescription() {
}

public ItemMetadataAssert hasSourceType(String type) {
objects.assertEqual(this.info, this.actual.getSourceType(), type);
extracting(ItemMetadata::getSourceType).isEqualTo(type);
return this;
}

Expand All @@ -82,12 +84,12 @@ public ItemMetadataAssert hasSourceType(Class<?> type) {
}

public ItemMetadataAssert hasSourceMethod(String type) {
objects.assertEqual(this.info, this.actual.getSourceMethod(), type);
extracting(ItemMetadata::getSourceMethod).isEqualTo(type);
return this;
}

public ItemMetadataAssert hasDefaultValue(Object defaultValue) {
objects.assertEqual(this.info, this.actual.getDefaultValue(), defaultValue);
extracting(ItemMetadata::getDefaultValue).isEqualTo(defaultValue);
return this;
}

Expand All @@ -97,27 +99,28 @@ public ItemMetadataAssert isDeprecatedWithNoInformation() {
}

public ItemMetadataAssert isDeprecatedWithReason(String reason) {
ItemDeprecation deprecation = assertItemDeprecation();
objects.assertEqual(this.info, deprecation.getReason(), reason);
assertItemDeprecation().extracting(ItemDeprecation::getReason).isEqualTo(reason);
return this;
}

public ItemMetadataAssert isDeprecatedWithReplacement(String replacement) {
ItemDeprecation deprecation = assertItemDeprecation();
objects.assertEqual(this.info, deprecation.getReplacement(), replacement);
assertItemDeprecation().extracting(ItemDeprecation::getReplacement).isEqualTo(replacement);
return this;
}

public ItemMetadataAssert isNotDeprecated() {
objects.assertNull(this.info, this.actual.getDeprecation());
extracting(ItemMetadata::getDeprecation).isNull();
return this;
}

private ItemDeprecation assertItemDeprecation() {
ItemDeprecation deprecation = this.actual.getDeprecation();
objects.assertNotNull(this.info, deprecation);
objects.assertNull(this.info, deprecation.getLevel());
return deprecation;
private ObjectAssert<ItemDeprecation> assertItemDeprecation() {
ObjectAssert<ItemDeprecation> itemDeprecationAssert = extracting(ItemMetadata::getDeprecation);
itemDeprecationAssert.extracting(ItemDeprecation::getLevel).isNull();
return itemDeprecationAssert;
}

private <T> ObjectAssert<T> extracting(Function<ItemMetadata, T> extractor) {
return super.extracting(extractor, Assertions::assertThat);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@

package org.springframework.boot.test.json;

import java.util.Map;

import org.assertj.core.api.AbstractMapAssert;
import org.assertj.core.api.AbstractObjectArrayAssert;
import org.assertj.core.api.AbstractObjectAssert;
import org.assertj.core.api.Assert;
import org.assertj.core.api.Assertions;
import org.assertj.core.internal.Objects;
import org.assertj.core.api.InstanceOfAssertFactories;

/**
* AssertJ {@link Assert} for {@link ObjectContent}.
*
* @param <A> the actual type
* @author Phillip Webb
* @author Stefano Cordio
* @since 1.4.0
*/
public class ObjectContentAssert<A> extends AbstractObjectAssert<ObjectContentAssert<A>, A> {
Expand All @@ -44,19 +42,16 @@ protected ObjectContentAssert(A actual) {
* @return an array assertion object
*/
public AbstractObjectArrayAssert<?, Object> asArray() {
Objects.instance().assertIsInstanceOf(this.info, this.actual, Object[].class);
return Assertions.assertThat((Object[]) this.actual);
return asInstanceOf(InstanceOfAssertFactories.ARRAY);
}

/**
* Verifies that the actual value is a map, and returns a map assertion, to allow
* chaining of map-specific assertions from this call.
* @return a map assertion object
*/
@SuppressWarnings("unchecked")
public AbstractMapAssert<?, ?, Object, Object> asMap() {
Objects.instance().assertIsInstanceOf(this.info, this.actual, Map.class);
return Assertions.assertThat((Map<Object, Object>) this.actual);
return asInstanceOf(InstanceOfAssertFactories.MAP);
}

}