Posted in

用 OpenRewrite 消除繁琐_AI阅读总结 — 包阅AI

包阅导读总结

1.

关键词:OpenRewrite、refactoring、development、automation、JUnit 5

2.

总结:本文主要介绍了 OpenRewrite 工具可消除开发中的繁琐工作,以将 JUnit 4 测试更新到 JUnit 5 为例,说明了其便捷性和高效性,包括运行方式、迁移结果及节省的时间。

3.

主要内容:

– 作者认为一些任务很繁琐,如更新测试到 JUnit 5 。

– 介绍 OpenRewrite 是用于重构 Java 代码的工具。

– 运行相关食谱可对源代码进行转换,有丰富的食谱集合。

– 提到曾用于迁移应用到 Spring Boot 3 。

– 运行 OpenRewrite 的方式

– 有多种方式,作者倾向使用 Gradle 初始化脚本。

– 给出了 JUnit 4 到 JUnit 5 迁移的 Gradle 初始化脚本。

– 运行迁移的命令及结果

– 展示运行 OpenRewrite 的命令。

– 迁移后对代码的多处修改,作者仅需更改部分测试插件。

– 该工具估计为作者节省 55 分钟工作时间,并提供了迁移项目的脚本。

思维导图:

文章地址:http://microservices.io//post/architecture/2024/08/06/eliminating-tedium-with-openrewrite.html

文章来源:microservices.io

作者:Chris Richardson

发布时间:2024/8/6 8:03

语言:英文

总字数:450字

预计阅读时间:2分钟

评分:91分

标签:OpenRewrite,Java 重构,JUnit 迁移,Gradle,软件开发效率


以下为原文内容

本内容来源于用户推荐转载,旨在分享知识与观点,如有侵权请联系删除 联系邮箱 media@ilingban.com

Eliminating tedium with OpenRewrite

refactoring development developer experience automation


Public workshops: Sept 4-6th & 23rd-25th – Architecting for fast, sustainable flow – enabling DevOps and Team Topologies thru architecture. Learn more and enroll.


I find some tasks so tedious that I tend to avoid them.For example, I’ve recently been working on a sample application for a new, soon to be released Eventuate saga framework (more on that later).I’d created the application by copying another sample that was still using JUnit 4.But I’d been putting off updating the tests to JUnit 5.That is, until today, when I got it done in a few minutes using OpenRewrite.

What is OpenRewrite?

OpenRewrite is a tool for refactoring Java code.It runs recipes that transform your source code.By running a single command, you can quickly and reliably make what would otherwise be very time-consuming changes to your code base.

There is a comprehensive collection of recipes available.I’d previously used it to migrate an application to Spring Boot 3.There’s also a recipe for migrating a Spring Boot application from JUnit 4 to JUnit 5.

Running OpenRewrite

There are a few different ways to run OpenRewrite.Many examples modify your pom.xml or build.gradle files.But since you typically run a recipe once, my preferred approach is to use a Gradle init script.This allows me to run OpenRewrite from the command line without modifying my project.

The Gradle init script for running OpenRewrite

Here’s the Gradle init script (copied from the OpenRewrite docs) that I used to run the JUnit 4 to JUnit 5 migration recipe:

initscript {    repositories {        maven { url "https://plugins.gradle.org/m2" }    }    dependencies {        classpath("org.openrewrite:plugin:latest.release")    }}rootProject {    plugins.apply(org.openrewrite.gradle.RewritePlugin)    dependencies {        rewrite("org.openrewrite.recipe:rewrite-spring:latest.release")    }    afterEvaluate {        if (repositories.isEmpty()) {            repositories {                mavenCentral()            }        }    }}

The script declares org.openrewrite.recipe:rewrite-spring, which contains the needed recipe, as a dependency.

Running the recipe

Here’s the shell command that I used to run OpenRewrite:

 ./gradlew rewriteRun --init-script <path to initscript> \    -Drewrite.activeRecipe=org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration \    -Dorg.gradle.jvmargs=-Xmx8G

This command runs the rewriteRun task, which is defined by the init script.The rewrite.activeRecipe property specifies the recipe to run.I needed to increase the heap size to prevent OpenRewrite from running out of memory.

The results

The recipe made numerous changes to the code base including:

  • Updating the build.gradle dependencies
  • Fixing the imports
  • Removing @RunWith(SpringJUnit4ClassRunner.class) annotations
  • Replacing @RunWith(MockitoJUnitRunner.class) with @ExtendWith(MockitoExtension.class)
  • Replacing @Before with @BeforeEach

The only thing I needed to do was change some testing related plugins to use useJUnitPlatform().

Pretty awesome!OpenRewrite estimated that it saved me 55 minutes of work.

The code

Here’s a handy shell script that migrates your project from JUnit 4 to JUnit 5.

Need help with accelerating software delivery?

I’m available to help your organization improve agility and competitiveness through better software architecture: training workshops, architecture reviews, etc.

Learn more about how I can help