Skip to content

Commit

Permalink
Convert Hamcrest not(hasItem(item)) to AssertJ doesNotContain(item)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Mar 13, 2024
1 parent 409f50a commit 7fb2c9d
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 56 deletions.
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/rewrite/hamcrest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ recipeList:
notMatcher: emptyString
assertion: isNotEmpty

- org.openrewrite.java.testing.hamcrest.HamcrestNotMatcherToAssertJ:
notMatcher: hasItem
assertion: doesNotContain

# Add dependency if not already present
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.assertj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,38 @@ void notMatcher() {
spec -> spec.recipe(new HamcrestNotMatcherToAssertJ("equalTo", "isNotEqualTo")),
//language=java
java(
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.equalTo;
class ATest {
@Test
void test() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1, not(equalTo(str2)));
}
}
""", """
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ATest {
@Test
void test() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1).isNotEqualTo(str2);
}
}
"""));
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.equalTo;
class ATest {
@Test
void test() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1, not(equalTo(str2)));
}
}
""",
"""
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ATest {
@Test
void test() {
String str1 = "Hello world!";
String str2 = "Hello world!";
assertThat(str1).isNotEqualTo(str2);
}
}
"""
)
);
}

@Test
Expand All @@ -79,32 +82,79 @@ void notMatcherWithReason() {
spec -> spec.recipe(new HamcrestNotMatcherToAssertJ("nullValue", "isNotNull")),
//language=java
java(
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
class ATest {
@Test
void test() {
String str1 = "Hello world!";
assertThat("Reason", str1, not(nullValue()));
}
}
""", """
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ATest {
@Test
void test() {
String str1 = "Hello world!";
assertThat(str1).as("Reason").isNotNull();
}
}
"""));
"""
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
class ATest {
@Test
void test() {
String str1 = "Hello world!";
assertThat("Reason", str1, not(nullValue()));
}
}
""",
"""
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ATest {
@Test
void test() {
String str1 = "Hello world!";
assertThat(str1).as("Reason").isNotNull();
}
}
"""
)
);
}

@Test
void notMatcherWithCollection() {
rewriteRun(
spec -> spec.recipe(new HamcrestNotMatcherToAssertJ("hasItem", "doesNotContain")),
//language=java
java(
"""
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.hasItem;
class ATest {
@Test
void test() {
List<String> str1 = List.of("Hello world!");
String str2 = "Hello world!";
assertThat(str1, not(hasItem(str2)));
}
}
""",
"""
import java.util.List;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class ATest {
@Test
void test() {
List<String> str1 = List.of("Hello world!");
String str2 = "Hello world!";
assertThat(str1).doesNotContain(str2);
}
}
"""
)
);
}
}

0 comments on commit 7fb2c9d

Please sign in to comment.