@Override
    @NonNull
    public Statement apply(@NonNull Statement base, @NonNull Description description) {
        return new Statement() {
            @Override
            public void evaluate() throws Throwable {
                final NeedsMockWebServer needsMockWebServer = description.getAnnotation(NeedsMockWebServer.class);

                if (needsMockWebServer != null) {
                    final MockWebServer mockWebServer = new MockWebServer();
                    mockWebServer.start();

                    TestUtils.app().applicationComponent().changeableBaseUrl().setBaseUrl(mockWebServer.url("").toString());

                    if (!needsMockWebServer.setupMethod().isEmpty()) {
                        final Method setupMethod = testClassInstance.getClass().getDeclaredMethod(needsMockWebServer.setupMethod(), MockWebServer.class);
                        setupMethod.invoke(testClassInstance, mockWebServer);
                    }

                    // Try to evaluate the test and anyway shutdown the MockWebServer.
                    try {
                        base.evaluate();
                    } finally {
                        mockWebServer.shutdown();
                    }
                } else {
                    // No need to setup a MockWebServer, just evaluate the test.
                    base.evaluate();
                }
            }
        };
    }
JUnit test rule for mocking web server!
app/src/functionalTests/java/com/artemzin/qualitymatters/functional_tests/rules/MockWebServerRule.java