title: Spring Boot Test with Zuul filters is not picking up new instance of bean
body: <p>I am using @SpringBootTest to run integration level tests with Zuul. Unfortunately I am running into an issue with the ApplicationContext refresh. Multiple instances of my RouteLocator is being created and the current instance isn't always being used. The RouteLocator I have implemented also implements the RefreshableRouteLocator. I created a very simple static field that increments by 1 every time an instance is created.</p>
  
  <pre><code>private static int id = 0;
  private int instanceId = id++;
  </code></pre>
  
  <p>The tests leverage WireMock to mock the backend service that is being called from the the Zuul route. WireMock is configured to use a random port so I have @Before and @After methods to modify the route within the RouteLocator for each test.</p>
  
  <pre><code>@Autowired
  protected LiveMigrationRouteLocator liveMigrationRouteLocator;
  
  @Rule
  public WireMockRule serviceA = new WireMockRule(Options.DYNAMIC_PORT);
  
  @Rule
  public WireMockRule serviceB = new WireMockRule(Options.DYNAMIC_PORT);
  
  @Before
  public void before() {
      LiveMigrationProperties routingProperties = liveMigrationRouteLocator.getRoutingProperties();
      for (LiveMigrationRoute route : routingProperties.getRoutes().values()) {
          route.setPrimaryUrl(route.getPrimaryUrl().replace("serviceA", "localhost:" + serviceA.port())
                  .replace("serviceB", "localhost:" + serviceB.port()));
          route.setSecondaryUrl(route.getSecondaryUrl().replace("serviceA", "localhost:" + serviceA.port())
                  .replace("serviceB", "localhost:" + serviceB.port()));
      }
      liveMigrationRouteLocator.setRoutingProperties(routingProperties);
      liveMigrationRouteLocator.refresh();
  
      serviceA.stubFor(get(urlPathMatching("/test-path/.*")).willReturn(aResponse()
              .withHeader("Content-Type", "application/json").withStatus(200).withBody("serviceA:test-path")));
      serviceB.stubFor(get(urlPathMatching("/test-path/.*")).willReturn(aResponse()
              .withHeader("Content-Type", "application/json").withStatus(200).withBody("serviceB:test-path")));
  }
  
  @After
  public void after() {
      LiveMigrationProperties routingProperties = liveMigrationRouteLocator.getRoutingProperties();
      for (LiveMigrationRoute route : routingProperties.getRoutes().values()) {
          route.setPrimaryUrl(route.getPrimaryUrl().replace("localhost:" + serviceA.port(), "serviceA")
                  .replace("localhost:" + serviceB.port(), "serviceB"));
          route.setSecondaryUrl(route.getSecondaryUrl().replace("localhost:" + serviceA.port(), "serviceA")
                  .replace("localhost:" + serviceB.port(), "serviceB"));
      }
      liveMigrationRouteLocator.setRoutingProperties(routingProperties);
      liveMigrationRouteLocator.refresh();
  }
  </code></pre>
  
  <p>I have multiple tests classes extending a base class that has the above implementation. Each test class has the following annotations:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
  @EnableConfigurationProperties(LiveMigrationProperties.class)
  public class RoutingTests extends LiveMigrationWireMockTests {
  </code></pre>
  
  <p>When running the tests I have debug logging around the refresh that occurs to see the issue. The log is at <a href="http://pastebin.com/preMTZ22" rel="nofollow">http://pastebin.com/preMTZ22</a>. If you look for "Refreshing routes [#]" where # is the instance of the RouteLocator that is being used. The first test passes fine and you can see that it is using instance #0. Then the next test class runs and the ApplicationContext is refreshed so a new instance is created. You can see the "Refreshing routes [1]" along with "Get matching route [1]: ..." for each route that is looked up from the SimpleHostRoutingFilter.</p>
  
  <p>Now the problem shows up when the next test class runs. The ApplicationContext refreshes again so the "Refreshing routes [2]" show up. When the SimpleHostRoutingFilter calls the RouteLocator you can see the log for "Getting matching route [1]: ...". This should be "Getting matching route [2]: ...". Due to this the SimpleHostRoutingFilter fails as it is trying to call <a href="http://serviceA" rel="nofollow">http://serviceA</a> instead of the dynamic port for the WireMock.</p>
  
  <p>I have tried to recreate this in a simple test but I can't seem to reproduce it. The simple test does show the multiple instances being created but when it comes to doing the test only instance #0 is the one interacted with. <a href="http://pastebin.com/9jiCRmRE" rel="nofollow">http://pastebin.com/9jiCRmRE</a></p>
  
  <p><a href="https://github.com/ShawnTuatara/stack-overflow-spring-boot-test-zuul-autowired-instance" rel="nofollow">https://github.com/ShawnTuatara/stack-overflow-spring-boot-test-zuul-autowired-instance</a></p>