Skip to content
Merged
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 @@ -60,6 +60,11 @@ public class TestsShouldIncludeAssertions extends Recipe {
"reactor.test.StepVerifier"
);

private static final List<MethodMatcher> ASSUMPTIONS = Arrays.asList(
new MethodMatcher("org.junit.jupiter.api.Assumptions *(..)"),
new MethodMatcher("org.junit.Assume *(..)")
);

@Option(displayName = "Additional assertions",
description = "A comma delimited list of packages and/or classes that will be identified as assertions. I.E. a common assertion utility `org.foo.TestUtil`.",
example = "org.foo.TestUtil, org.bar",
Expand All @@ -69,7 +74,7 @@ public class TestsShouldIncludeAssertions extends Recipe {

String displayName = "Include an assertion in tests";

String description = "For tests not having any assertions, wrap the statements with JUnit Jupiter's `Assertions#assertDoesNotThrow(..)`.";
String description = "For tests not having any assertions, wrap the statements with JUnit Jupiter's `Assertions#assertDoesNotThrow(..)`. Tests that make an assumption are left unchanged, as wrapping them would report a skipped test as a failure.";

Set<String> tags = singleton("RSPEC-S2699");

Expand Down Expand Up @@ -159,7 +164,7 @@ private boolean methodHasAssertion(J.Block body) {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) {
J.MethodInvocation mi = super.visitMethodInvocation(method, atomicBoolean);
if (isAssertion(mi)) {
if (isAssertion(mi) || isAssumption(mi)) {
atomicBoolean.set(Boolean.TRUE);
}
return mi;
Expand Down Expand Up @@ -214,6 +219,10 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Se
return findMethodDeclarationVisitor.reduce(classDeclaration, new HashSet<>());
}

private boolean isAssumption(J.MethodInvocation methodInvocation) {
return ASSUMPTIONS.stream().anyMatch(matcher -> matcher.matches(methodInvocation));
}

private boolean isAssertion(J.MethodInvocation methodInvocation) {
if (methodInvocation.getMethodType() == null) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/rewrite/recipes.csv
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.tes
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.RemoveTestPrefix,Remove `test` prefix from JUnit 5 tests,"Remove `test` from methods with `@Test`, `@ParameterizedTest`, `@RepeatedTest` or `@TestFactory`. They no longer have to prefix test to be usable by JUnit 5.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,,
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.SimplifyTestThrows,Simplify `throws` statements of tests,Replace all thrown exception classes of test method signatures by `Exception`.,1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,,
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.TestMethodsShouldBeVoid,Test methods should have void return type,"Test methods annotated with `@Test`, `@ParameterizedTest`, `@RepeatedTest`, `@TestTemplate` should have `void` return type. Non-void return types can cause test discovery issues, and warnings as of JUnit 5.13+. This recipe changes the return type to `void` and removes `return` statements.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,,
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.TestsShouldIncludeAssertions,Include an assertion in tests,"For tests not having any assertions, wrap the statements with JUnit Jupiter's `Assertions#assertDoesNotThrow(..)`.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,"[{""name"":""additionalAsserts"",""type"":""String"",""displayName"":""Additional assertions"",""description"":""A comma delimited list of packages and/or classes that will be identified as assertions. I.E. a common assertion utility `org.foo.TestUtil`."",""example"":""org.foo.TestUtil, org.bar""}]",
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.TestsShouldIncludeAssertions,Include an assertion in tests,"For tests not having any assertions, wrap the statements with JUnit Jupiter's `Assertions#assertDoesNotThrow(..)`. Tests that make an assumption are left unchanged, as wrapping them would report a skipped test as a failure.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,"[{""name"":""additionalAsserts"",""type"":""String"",""displayName"":""Additional assertions"",""description"":""A comma delimited list of packages and/or classes that will be identified as assertions. I.E. a common assertion utility `org.foo.TestUtil`."",""example"":""org.foo.TestUtil, org.bar""}]",
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.TestsShouldNotBePublic,Remove `public` visibility of JUnit 5 tests,"Remove `public` and optionally `protected` modifiers from methods with `@Test`, `@ParameterizedTest`, `@RepeatedTest`, `@TestFactory`, `@BeforeEach`, `@AfterEach`, `@BeforeAll`, or `@AfterAll`. They no longer have to be public visibility to be usable by JUnit 5.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,"[{""name"":""removeProtectedModifiers"",""type"":""Boolean"",""displayName"":""Remove protected modifiers"",""description"":""Also remove protected modifiers from test methods"",""example"":""true""}]",
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.datafaker.JavaFakerToDataFaker,Migrate from Java Faker to Datafaker,Change imports and dependencies related to Java Faker to Datafaker replacements.,6,DataFaker,Testing,Java,Recipes for migrating from JavaFaker to [DataFaker](https://www.datafaker.net/).,,Basic building blocks for transforming Java code.,,
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.dbrider.ExecutionListenerToDbRiderAnnotation,Migrate the `DBRiderTestExecutionListener` to the `@DBRider` annotation,Migrate the `DBRiderTestExecutionListener` to the `@DBRider` annotation. This recipe is useful when migrating from JUnit 4 `dbrider-spring` to JUnit 5 `dbrider-junit5`.,1,DBRider,Testing,Java,Recipes for [DBRider](https://database-rider.github.io/database-rider/) database testing framework.,,Basic building blocks for transforming Java code.,,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,66 @@ public void methodTest() {
);
}

@Test
void ignoreJupiterAssumption() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
class AaTest {
@Test
void onLinux() {
assumeTrue(System.getProperty("os.name").contains("Linux"));
System.out.println("start");
}
}
"""
)
);
}

@Test
void ignoreJUnit4Assume() {
//language=java
rewriteRun(
java(
"""
import org.junit.Assume;
import org.junit.jupiter.api.Test;
class AaTest {
@Test
void onLinux() {
Assume.assumeTrue(System.getProperty("os.name").contains("Linux"));
System.out.println("start");
}
}
"""
)
);
}

@Test
void ignoreAssertJAssumption() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assumptions.assumeThat;
class AaTest {
@Test
void onLinux() {
assumeThat(System.getProperty("os.name")).contains("Linux");
System.out.println("start");
}
}
"""
)
);
}

@Test
void ignoreEmpty() {
//language=java
Expand Down
Loading