This appears to be related to

  • https://github.com/spring-projects/spring-framework/commit/dbe89abd7b39217044b3e021b4fe740c7ef88a4b
  • https://github.com/spring-projects/spring-framework/issues/35171
  • https://github.com/spring-projects/spring-framework/issues/35168

Adding @DirtiesContext works around the issue

Note that part of the problem is a little challenging when using an ephemeral port. We cannot just start and restart the context because:

1) If we allocate a new port then Beans that used the previous port information will be stale 2) If we preserve the same port, the port may have been taken in the meantime

I spoke to @jhoeller and @sbrannen offline and it was suggested that we might be able to do something like this:

public void stop() {
    if (port == 0 && context != null && !context.isClosed()) return;
}