Skip to content

Correcting MockWebServer migration for OkHttp#849

Merged
timtebeek merged 41 commits intomainfrom
1589-correcting-mockwebserver-migration
Jan 22, 2026
Merged

Correcting MockWebServer migration for OkHttp#849
timtebeek merged 41 commits intomainfrom
1589-correcting-mockwebserver-migration

Conversation

@steve-aom-elliott
Copy link
Contributor

@steve-aom-elliott steve-aom-elliott commented Nov 21, 2025

What's changed?

  • New artifactId corrected from mockwebserver3-junit5 to mockwebserver3
  • In-progress work for changing how some classes are built / manipulated. Previously there were many methods existing on MockResponse. Now everything has to be switched to using MockResponse.Builder to change all these values.

What's your motivation?

Compilation failures after previous setup's migration ran.

Anything in particular you'd like reviewers to focus on?

This PR covers most of the MockResponse setter and other methods where there are analogous methods on MockResponse.Builder, with the exception of setSocketPolicy. The setSocketPolicy would map onto one of the onResponse methods in the builder but this requires mapping SocketPolicy to SocketEffect and also choosing the right onResponse method.

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@steve-aom-elliott steve-aom-elliott self-assigned this Nov 21, 2025
@steve-aom-elliott steve-aom-elliott added bug Something isn't working dependencies Pull requests that update a dependency file test provided junit labels Nov 21, 2025
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Nov 21, 2025
@steve-aom-elliott steve-aom-elliott force-pushed the 1589-correcting-mockwebserver-migration branch 3 times, most recently from 630c51b to 58ef5a9 Compare November 27, 2025 21:47
@steve-aom-elliott
Copy link
Contributor Author

I'm going to try to look into whether I can set up a single visitor to perform all the type changes needed prior to using the JavaTemplate to write the .build() chaining in, as the problem I'm hitting here is that I'm changing MockResponse fields to MockResponse.Builder fields, return types from some of the methods of MockResponse to be on MockResponse.Builder instead, followed by introducing a .build() call that belongs to MockResponse.Builder but returns MockResponse, meaning ChangeType recipe effectively breaks the type attribution, but covers a lot of the scenarios otherwise that I'd want to be changing. Going to probably roll my own visitor that combines functionality from some of the other ones, or create a visitor that is capable of altering a method invocation's signature's parameter types, which I don't believe we have easily accessible a the moment.

@steve-aom-elliott steve-aom-elliott force-pushed the 1589-correcting-mockwebserver-migration branch from 58ef5a9 to 9f40caf Compare December 11, 2025 14:13
@steve-aom-elliott steve-aom-elliott force-pushed the 1589-correcting-mockwebserver-migration branch from 9f40caf to 7855d82 Compare December 12, 2025 22:09
@steve-aom-elliott steve-aom-elliott force-pushed the 1589-correcting-mockwebserver-migration branch from 7855d82 to 2cf1e69 Compare January 7, 2026 15:38
steve-aom-elliott and others added 5 commits January 15, 2026 11:31
…padding (including the newline), leading to it changing wrapping.
…uild()`, and backpatching the parameter types onto the various method invocations. Feels like there could be a lot of improvement still with how this done, as it feels pretty hacky right now.
@dsgrieve dsgrieve marked this pull request as ready for review January 21, 2026 23:46
Copy link
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Thanks both! Find quite a few different ways to trim this down still; see the individual commits for details. From this minimized base I think we can now look to cover additional cases with reduced complexity.

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Jan 22, 2026
Comment on lines +213 to +234
void configureFully(MockResponse.Builder mockResponse) {
MockResponse.Builder mrA = mockResponse.addHeader("accept:application/json");
MockResponse.Builder mrB = mockResponse.addHeader("accept", "application/json");
MockResponse.Builder mrC = mockResponse.addHeaderLenient("accept", "application/json");
MockResponse.Builder mrD = mockResponse.removeHeader("accept");
Builder mrE = mockResponse.body("Lorem ipsum");
Builder mrF = mockResponse.body(new Buffer());
Builder mrG = mockResponse.bodyDelay(30L, TimeUnit.SECONDS);
Builder mrH = mockResponse.chunkedBody("Lorem ipsum", 2048);
Builder mrI = mockResponse.chunkedBody(new Buffer(), 2048);
MockResponse.Builder mrJ = mockResponse.setHeader("accept","application/json");
Builder mrK = mockResponse.headers(new Headers.Builder().add("accept:application/json").build());
Builder mrL = mockResponse.headersDelay(30L, TimeUnit.SECONDS);
Builder mrM = mockResponse.code(500);
Builder mrN = mockResponse.code(200);
Builder mrO = mockResponse.status("OK");
Builder mrP = mockResponse.trailers(new Headers.Builder().add("x-trailer:value").build());
MockResponse.Builder mrQ = mockResponse.throttleBody(1024, 1, TimeUnit.SECONDS);
Builder mrR = mockResponse.addPush(pushPromise);
Builder mrS = mockResponse.settings(settings);
Builder mrT = mockResponse.webSocketUpgrade(webSocketListener);
}
Copy link
Member

@timtebeek timtebeek Jan 22, 2026

Choose a reason for hiding this comment

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

The changes in imports and types here is because some of these are changed by map of renamed methods, whereas others are kept as is. In practice I'd expect these differences in types and imports to be rare, as the chained way of calling these methods is expected to be most prevalent.

@timtebeek timtebeek merged commit 7985d3a into main Jan 22, 2026
1 of 2 checks passed
@timtebeek timtebeek deleted the 1589-correcting-mockwebserver-migration branch January 22, 2026 19:44
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jan 22, 2026
mergify bot added a commit to robfrank/linklift that referenced this pull request Feb 14, 2026
…rom 3.26.0 to 3.27.0 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-testing-frameworks](https://github.com/openrewrite/rewrite-testing-frameworks) from 3.26.0 to 3.27.0.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-testing-frameworks's releases](https://github.com/openrewrite/rewrite-testing-frameworks/releases).*

> 3.27.0
> ------
>
> What's Changed
> --------------
>
> * Correcting MockWebServer migration for OkHttp by [`@​steve-aom-elliott`](https://github.com/steve-aom-elliott) in [openrewrite/rewrite-testing-frameworks#849](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/849)
> * Add `ReturnActual` recipe for AssertJ by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-testing-frameworks#891](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/891)
> * Add `MockConstructionToTryWithResources` recipe by [`@​jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-testing-frameworks#892](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/892)
> * Add CleanupKotlinJUnit5AssertionImports recipe by [`@​jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-testing-frameworks#893](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/893)
> * Skip Kotlin files in PowerMockitoMockStaticToMockito by [`@​jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-testing-frameworks#894](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/894)
> * Add test cases for `MockedConstruction` pattern by [`@​jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-testing-frameworks#895](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/895)
> * Update byte buddy to a Java 25 compatible version by [`@​Laurens-W`](https://github.com/Laurens-W) in [openrewrite/rewrite-testing-frameworks#897](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/897)
> * Followup after the formatting merge by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#900](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/900)
> * Skip TemporaryFolder conversion when used in RuleChain. by [`@​motlin`](https://github.com/motlin) in [openrewrite/rewrite-testing-frameworks#898](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/898)
> * Add JUnit 4 to JUnit 5 exception class migrations. by [`@​motlin`](https://github.com/motlin) in [openrewrite/rewrite-testing-frameworks#902](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/902)
> * Make declarative recipes singletons by [`@​sambsnyd`](https://github.com/sambsnyd) in [openrewrite/rewrite-testing-frameworks#905](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/905)
> * Handle method references in JUnit Assert to Assertions migration. by [`@​motlin`](https://github.com/motlin) in [openrewrite/rewrite-testing-frameworks#901](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/901)
> * Add explicit test scope to AssertJ dependency additions by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#903](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/903)
> * Skip SimplifyChainedAssertJAssertion when actual type has wildcard type parameters by [`@​knutwannheden`](https://github.com/knutwannheden) in [openrewrite/rewrite-testing-frameworks#906](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/906)
> * Revert "Add explicit test scope to AssertJ dependency additions ([#903](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/903))" by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#907](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/907)
> * Fix AssertThrowsOnLastStatement to preserve parameterized types when extracting arguments. by [`@​motlin`](https://github.com/motlin) in [openrewrite/rewrite-testing-frameworks#908](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/908)
> * Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks by [`@​dsgrieve`](https://github.com/dsgrieve) in [openrewrite/rewrite-testing-frameworks#904](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/904)
> * `RemoveInitMocksIfRunnersSpecified` (or any recipe) should not have a mutable static field by [`@​Jenson3210`](https://github.com/Jenson3210) in [openrewrite/rewrite-testing-frameworks#910](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/910)
>
> New Contributors
> ----------------
>
> * [`@​motlin`](https://github.com/motlin) made their first contribution in [openrewrite/rewrite-testing-frameworks#898](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/898)
> * [`@​dsgrieve`](https://github.com/dsgrieve) made their first contribution in [openrewrite/rewrite-testing-frameworks#904](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/pull/904)
>
> **Full Changelog**: <openrewrite/rewrite-testing-frameworks@v3.26.0...v3.27.0>


Commits

* [`41e6b42`](openrewrite/rewrite-testing-frameworks@41e6b42) Extract documentation examples from tests
* [`403dd48`](openrewrite/rewrite-testing-frameworks@403dd48) review comment ([#910](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/910))
* [`e6c383b`](openrewrite/rewrite-testing-frameworks@e6c383b) Delete files
* [`cf78c5a`](openrewrite/rewrite-testing-frameworks@cf78c5a) Enhance RemoveInitMocksIfRunnersSpecified to also remove openMocks ([#904](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/904))
* [`75c12cd`](openrewrite/rewrite-testing-frameworks@75c12cd) Fix AssertThrowsOnLastStatement to preserve parameterized types when extracti...
* [`07b43cd`](openrewrite/rewrite-testing-frameworks@07b43cd) Revert "Add explicit test scope to AssertJ dependency additions ([#903](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/903))" ([#907](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/907))
* [`3a67bb9`](openrewrite/rewrite-testing-frameworks@3a67bb9) Skip SimplifyChainedAssertJAssertion when actual type has wildcard type param...
* [`02eba0f`](openrewrite/rewrite-testing-frameworks@02eba0f) Add explicit test scope to AssertJ dependency additions ([#903](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/903))
* [`4e79908`](openrewrite/rewrite-testing-frameworks@4e79908) Handle method references in JUnit Assert to Assertions migration. ([#901](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/901))
* [`35409b0`](openrewrite/rewrite-testing-frameworks@35409b0) Make declarative recipes singletons ([#905](https://redirect.github.com/openrewrite/rewrite-testing-frameworks/issues/905))
* Additional commits viewable in [compare view](openrewrite/rewrite-testing-frameworks@v3.26.0...v3.27.0)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-testing-frameworks&package-manager=maven&previous-version=3.26.0&new-version=3.27.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working dependencies Pull requests that update a dependency file junit test provided

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants