openrewrite / rewrite-testing-frameworks

OpenRewrite recipes that perform common Java testing migration tasks.
Apache License 2.0
77 stars 73 forks source link

Recipe to migrate from system-rules to system-stubs #197

Open yeikel opened 2 years ago

yeikel commented 2 years ago

See : https://github.com/stefanbirkner/system-rules/issues/55

Sample changes :

Remove

import org.junit.Rule;
import org.junit.contrib.java.lang.system.EnvironmentVariables;

 @Rule
    public final EnvironmentVariables envVars = new EnvironmentVariables();

Remove the dependency :

<dependency>
    <groupId>com.github.stefanbirkner</groupId>
    <artifactId>system-rules</artifactId>
    <scope>test</scope>
</dependency>

It should add the dependency :

<dependency>
            <groupId>uk.org.webcompere</groupId>
            <artifactId>system-stubs-jupiter</artifactId>
            <scope>test</scope>
        </dependency>

It should also add the SystemStubsExtension at the class level

import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;

@ExtendWith(SystemStubsExtension.class)
public class testClass {

...
}

And add the environment as a parameter in the test

For example :

 @Test
    public void testBefore() {
            envVars.set("var", "100");
    }

  @Test
    public void testAfter(EnvironmentVariables envVars) {
            envVars.set("var", "100");
    }

Sample diff :

import io.vertx.core.json.JsonObject;
 import io.vertx.junit5.VertxExtension;
 import io.vertx.junit5.VertxTestContext;
-import org.junit.Rule;
-import org.junit.contrib.java.lang.system.EnvironmentVariables;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
+import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
+import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

 import java.io.IOException;

 @ExtendWith(VertxExtension.class)
+@ExtendWith(SystemStubsExtension.class)
 public class AccountSegmentFunctionVerticleTest {

-    @Rule
-    public final EnvironmentVariables envVars = new EnvironmentVariables();
     private final Vertx vertx;
@@ -50,14 +47,14 @@ public class TestClass {
     }

     @Test
-    public void test(VertxTestContext testContext) {
+    public void test(VertxTestContext testContext,final EnvironmentVariables envVars) {
         envVars.set("var", "100");
pway99 commented 2 years ago

thanks @yeikel, this is very helpful