Skip to content

[POC] Migrate JUnit asserts to AssertJ - impl #2307

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
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 @@ -20,8 +20,7 @@

import org.apache.maven.api.Version;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.assertj.core.api.Assertions.assertThat;

/**
*/
Expand All @@ -40,21 +39,21 @@ protected void assertOrder(int expected, String version1, String version2) {
Version v2 = newVersion(version2);

if (expected > 0) {
assertEquals(1, Integer.signum(v1.compareTo(v2)), "expected " + v1 + " > " + v2);
assertEquals(-1, Integer.signum(v2.compareTo(v1)), "expected " + v2 + " < " + v1);
assertNotEquals(v1, v2, "expected " + v1 + " != " + v2);
assertNotEquals(v2, v1, "expected " + v2 + " != " + v1);
assertThat(Integer.signum(v1.compareTo(v2))).as("expected " + v1 + " > " + v2).isEqualTo(1);
assertThat(Integer.signum(v2.compareTo(v1))).as("expected " + v2 + " < " + v1).isEqualTo(-1);
assertThat(v2).as("expected " + v1 + " != " + v2).isNotEqualTo(v1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't those messages automatically generated by assertJ in case of failures ?

assertThat(v1).as("expected " + v2 + " != " + v1).isNotEqualTo(v2);
} else if (expected < 0) {
assertEquals(-1, Integer.signum(v1.compareTo(v2)), "expected " + v1 + " < " + v2);
assertEquals(1, Integer.signum(v2.compareTo(v1)), "expected " + v2 + " > " + v1);
assertNotEquals(v1, v2, "expected " + v1 + " != " + v2);
assertNotEquals(v2, v1, "expected " + v2 + " != " + v1);
assertThat(Integer.signum(v1.compareTo(v2))).as("expected " + v1 + " < " + v2).isEqualTo(-1);
assertThat(Integer.signum(v2.compareTo(v1))).as("expected " + v2 + " > " + v1).isEqualTo(1);
assertThat(v2).as("expected " + v1 + " != " + v2).isNotEqualTo(v1);
assertThat(v1).as("expected " + v2 + " != " + v1).isNotEqualTo(v2);
} else {
assertEquals(0, v1.compareTo(v2), "expected " + v1 + " == " + v2);
assertEquals(0, v2.compareTo(v1), "expected " + v2 + " == " + v1);
assertEquals(v1, v2, "expected " + v1 + " == " + v2);
assertEquals(v2, v1, "expected " + v2 + " == " + v1);
assertEquals(v1.hashCode(), v2.hashCode(), "expected #(" + v1 + ") == #(" + v1 + ")");
assertThat(v1.compareTo(v2)).as("expected " + v1 + " == " + v2).isEqualTo(0);
assertThat(v2.compareTo(v1)).as("expected " + v2 + " == " + v1).isEqualTo(0);
assertThat(v2).as("expected " + v1 + " == " + v2).isEqualTo(v1);
assertThat(v1).as("expected " + v2 + " == " + v1).isEqualTo(v2);
assertThat(v2.hashCode()).as("expected #(" + v1 + ") == #(" + v1 + ")").isEqualTo(v1.hashCode());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
import org.eclipse.aether.util.version.GenericVersionScheme;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.assertj.core.api.Assertions.assertThat;

class DefaultModelVersionParserTest {

@Test
void parseVersion() {
Version v = new DefaultModelVersionParser(new GenericVersionScheme()).parseVersion("");
assertNotNull(v);
assertEquals("", v.toString());
assertThat(v).isNotNull();
assertThat(v.toString()).isEqualTo("");
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any isEmpty() assertion ?

Copy link

Choose a reason for hiding this comment

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

Is there any isEmpty() assertion ?

If not, you can create custom assertions for AssertJ very easly. I do this quite a lot at work for custom objects/classes

Copy link
Contributor Author

@Pankraz76 Pankraz76 May 11, 2025

Choose a reason for hiding this comment

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

might be a bug, as failed to choose: https://docs.openrewrite.org/recipes/java/testing/assertj/simplifyassertjassertion

@timtebeek

Or simply need to chain.

Copy link
Contributor Author

@Pankraz76 Pankraz76 May 11, 2025

Choose a reason for hiding this comment

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

Any change not standardized is weakened.

Simplest way would be JUnit Jupiter best practices and allow both.

or (continuous) migrate JUnit asserts to AssertJ like seen here.

Both will cure generic assertTrue statements elevate into assertThat.
Which lib is actually an random impl. detail.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;

class DefaultModelXmlFactoryTest {

Expand All @@ -40,7 +39,7 @@ void setUp() {
}

@Test
void testValidNamespaceWithModelVersion400() throws Exception {
void validNamespaceWithModelVersion400() throws Exception {
String xml =
"""
<project xmlns="http://maven.apache.org/POM/4.0.0">
Expand All @@ -51,12 +50,12 @@ void testValidNamespaceWithModelVersion400() throws Exception {
XmlReaderRequest.builder().reader(new StringReader(xml)).build();

Model model = factory.read(request);
assertEquals("4.0.0", model.getModelVersion());
assertEquals("http://maven.apache.org/POM/4.0.0", model.getNamespaceUri());
assertThat(model.getModelVersion()).isEqualTo("4.0.0");
assertThat(model.getNamespaceUri()).isEqualTo("http://maven.apache.org/POM/4.0.0");
}

@Test
void testValidNamespaceWithModelVersion410() throws Exception {
void validNamespaceWithModelVersion410() throws Exception {
String xml =
"""
<project xmlns="http://maven.apache.org/POM/4.1.0">
Expand All @@ -67,12 +66,12 @@ void testValidNamespaceWithModelVersion410() throws Exception {
XmlReaderRequest.builder().reader(new StringReader(xml)).build();

Model model = factory.read(request);
assertEquals("4.1.0", model.getModelVersion());
assertEquals("http://maven.apache.org/POM/4.1.0", model.getNamespaceUri());
assertThat(model.getModelVersion()).isEqualTo("4.1.0");
assertThat(model.getNamespaceUri()).isEqualTo("http://maven.apache.org/POM/4.1.0");
}

@Test
void testInvalidNamespaceWithModelVersion410() {
void invalidNamespaceWithModelVersion410() {
String xml =
"""
<project xmlns="http://invalid.namespace/4.1.0">
Expand All @@ -82,13 +81,13 @@ void testInvalidNamespaceWithModelVersion410() {
XmlReaderRequest request =
XmlReaderRequest.builder().reader(new StringReader(xml)).build();

XmlReaderException ex = assertThrows(XmlReaderException.class, () -> factory.read(request));
assertTrue(ex.getMessage().contains("Invalid namespace 'http://invalid.namespace/4.1.0'"));
assertTrue(ex.getMessage().contains("4.1.0"));
XmlReaderException ex = assertThatExceptionOfType(XmlReaderException.class).isThrownBy(() -> factory.read(request)).actual();
assertThat(ex.getMessage().contains("Invalid namespace 'http://invalid.namespace/4.1.0'")).isTrue();
assertThat(ex.getMessage().contains("4.1.0")).isTrue();
}

@Test
void testNoNamespaceWithModelVersion400() throws Exception {
void noNamespaceWithModelVersion400() throws Exception {
String xml =
"""
<project>
Expand All @@ -99,17 +98,17 @@ void testNoNamespaceWithModelVersion400() throws Exception {
XmlReaderRequest.builder().reader(new StringReader(xml)).build();

Model model = factory.read(request);
assertEquals("4.0.0", model.getModelVersion());
assertEquals("", model.getNamespaceUri());
assertThat(model.getModelVersion()).isEqualTo("4.0.0");
assertThat(model.getNamespaceUri()).isEqualTo("");
}

@Test
void testNullRequest() {
assertThrows(IllegalArgumentException.class, () -> factory.read((XmlReaderRequest) null));
void nullRequest() {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> factory.read((XmlReaderRequest) null));
}

@Test
void testMalformedModelVersion() throws Exception {
void malformedModelVersion() throws Exception {
String xml =
"""
<project xmlns="http://maven.apache.org/POM/4.0.0">
Expand All @@ -120,6 +119,6 @@ void testMalformedModelVersion() throws Exception {
XmlReaderRequest.builder().reader(new StringReader(xml)).build();

Model model = factory.read(request);
assertEquals("invalid.version", model.getModelVersion());
assertThat(model.getModelVersion()).isEqualTo("invalid.version");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;

class DefaultNodeTest {

@Test
void testAsString() {
void asString() {
InternalSession session = Mockito.mock(InternalSession.class);

// Create a basic dependency node
Expand All @@ -42,28 +42,26 @@ void testAsString() {

// Test non-verbose mode
DefaultNode defaultNode = new DefaultNode(session, node, false);
assertEquals("org.example:myapp:jar:1.0:compile", defaultNode.asString());
assertThat(defaultNode.asString()).isEqualTo("org.example:myapp:jar:1.0:compile");

// Test verbose mode with managed version
node.setData(DependencyManagerUtils.NODE_DATA_PREMANAGED_VERSION, "0.9");
node.setManagedBits(DependencyNode.MANAGED_VERSION);
defaultNode = new DefaultNode(session, node, true);
assertEquals("org.example:myapp:jar:1.0:compile (version managed from 0.9)", defaultNode.asString());
assertThat(defaultNode.asString()).isEqualTo("org.example:myapp:jar:1.0:compile (version managed from 0.9)");

// Test verbose mode with managed scope
node.setData(DependencyManagerUtils.NODE_DATA_PREMANAGED_SCOPE, "runtime");
node.setManagedBits(DependencyNode.MANAGED_VERSION | DependencyNode.MANAGED_SCOPE);
defaultNode = new DefaultNode(session, node, true);
assertEquals(
"org.example:myapp:jar:1.0:compile (version managed from 0.9; scope managed from runtime)",
defaultNode.asString());
assertThat(defaultNode.asString()).isEqualTo("org.example:myapp:jar:1.0:compile (version managed from 0.9; scope managed from runtime)");

// Test verbose mode with conflict resolution
DefaultDependencyNode winner =
new DefaultDependencyNode(new Dependency(new DefaultArtifact("org.example:myapp:2.0"), "compile"));
node.setData(ConflictResolver.NODE_DATA_WINNER, winner);
node.setManagedBits(0);
defaultNode = new DefaultNode(session, node, true);
assertEquals("(org.example:myapp:jar:1.0:compile - omitted for conflict with 2.0)", defaultNode.asString());
assertThat(defaultNode.asString()).isEqualTo("(org.example:myapp:jar:1.0:compile - omitted for conflict with 2.0)");
}
}
Loading