title: Can not use Spring Cloud Contract Wiremock, Failed to load ApplicationContext
body: <p>I tried to use Spring Cloud Contract Wiremock to test Feign client in microservce architecture with stub server, without real request to another server.
  I take the example from repository <strong><a href="https://github.com/spring-cloud/spring-cloud-contract/tree/master/samples/wiremock-undertow/src/test/java/com/example" rel="nofollow noreferrer">https://github.com/spring-cloud/spring-cloud-contract/tree/master/samples/wiremock-undertow/src/test/java/com/example</a></strong>, just change real data for my project. My code:</p>
  <pre><code>import com.fasterxml.jackson.core.type.TypeReference;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.gaming.jps.configuration.FeignMocks;
  import com.gaming.jps.dto.paymentSystem.PaymentSystemFieldsDto;
  import org.junit.jupiter.api.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
  import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
  import org.springframework.core.io.ClassPathResource;
  import org.springframework.test.context.ActiveProfiles;
  
  import java.util.Map;
  
  import static com.github.tomakehurst.wiremock.client.WireMock.*;
  import static java.nio.charset.Charset.defaultCharset;
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.springframework.util.StreamUtils.copyToString;
  
  
  //@RunWith(SpringRunner.class)
  @SpringBootTest(properties = &quot;app.baseUrl=http://localhost:7070&quot;, webEnvironment = WebEnvironment.NONE)
  @AutoConfigureWireMock(port = 7070)
  @ActiveProfiles(&quot;test&quot;)
  public class WiremockImportApplicationTests {
  
      @Autowired
      private Client client;
  
      private final ObjectMapper objectMapper = new ObjectMapper();
  
      @Test
      public void contextLoads() throws Exception {
  
          Map&lt;String, PaymentSystemFieldsDto&gt; response = objectMapper
                  .readValue(new ClassPathResource(FeignMocks.PPS_GET_PAYMENT_SYSTEM_INFO).getFile(),
                          new TypeReference&lt;Map&lt;String, PaymentSystemFieldsDto&gt;&gt;() {
                          });
  
          stubFor(get(urlEqualTo(&quot;/payment-systems-info&quot;))
                  .withHeader(Client.HEADER_AUTH, containing(&quot;PPS &quot;))
                  .willReturn(aResponse()
                              .withBody(copyToString(
                                      FeignMocks.class.getClassLoader().getResourceAsStream(FeignMocks.PPS_GET_PAYMENT_SYSTEM_INFO),
                                      defaultCharset()
                              ))));
          assertThat(client.paymentSystemsInfo(FeignMocks.PPS_KEY, null)).isEqualTo(response);
      }
  
  }
  </code></pre>
  <p>Dependency in POM:</p>
  <pre><code>        &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.cloud&lt;/groupId&gt;
              &lt;artifactId&gt;spring-cloud-starter-contract-stub-runner&lt;/artifactId&gt;
              &lt;version&gt;2.2.5.RELEASE&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  </code></pre>
  <p>But when I try to run this test I am receiving exception with loadin Context:</p>
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
  
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
      at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
      at org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener.prepareTestInstance(SpringBootDependencyInjectionTestExecutionListener.java:43)
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:244)
      at org.springframework.test.context.junit.jupiter.SpringExtension.postProcessTestInstance(SpringExtension.java:98)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$5(ClassBasedTestDescriptor.java:341)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.executeAndMaskThrowable(ClassBasedTestDescriptor.java:346)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeTestInstancePostProcessors$6(ClassBasedTestDescriptor.java:341)
      at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
      at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
      at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1655)
      at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
      at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
      at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
      at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735)
      at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:734)
      at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeTestInstancePostProcessors(ClassBasedTestDescriptor.java:340)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.instantiateAndPostProcessTestInstance(ClassBasedTestDescriptor.java:263)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$2(ClassBasedTestDescriptor.java:256)
      at java.base/java.util.Optional.orElseGet(Optional.java:369)
      at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$testInstancesProvider$3(ClassBasedTestDescriptor.java:255)
      at org.junit.jupiter.engine.execution.TestInstancesProvider.getTestInstances(TestInstancesProvider.java:29)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$prepare$0(TestMethodTestDescriptor.java:108)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:107)
      at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.prepare(TestMethodTestDescriptor.java:71)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$prepare$1(NodeTestTask.java:107)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.prepare(NodeTestTask.java:107)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:75)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
      at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
      at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
      at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
      at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
      at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
      at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
      at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
      at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
      at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
      at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:71)
      at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
      at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
      at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.contract.wiremock.WireMockConfiguration': Invocation of init method failed; nested exception is com.github.tomakehurst.wiremock.common.FatalStartupException: java.lang.RuntimeException: java.io.IOException: Failed to bind to /0.0.0.0:7070
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:415)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)
      at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
      at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
      at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
      at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
      ... 64 more
  Caused by: com.github.tomakehurst.wiremock.common.FatalStartupException: java.lang.RuntimeException: java.io.IOException: Failed to bind to /0.0.0.0:7070
      at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:148)
      at org.springframework.cloud.contract.wiremock.WireMockConfiguration.start(WireMockConfiguration.java:266)
      at org.springframework.cloud.contract.wiremock.WireMockConfiguration.reRegisterServer(WireMockConfiguration.java:165)
      at org.springframework.cloud.contract.wiremock.WireMockConfiguration.reRegisterServerWithResetMappings(WireMockConfiguration.java:181)
      at org.springframework.cloud.contract.wiremock.WireMockConfiguration.init(WireMockConfiguration.java:113)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.base/java.lang.reflect.Method.invoke(Method.java:566)
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
      at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
      ... 82 more
  Caused by: java.lang.RuntimeException: java.io.IOException: Failed to bind to /0.0.0.0:7070
      at com.github.tomakehurst.wiremock.jetty9.JettyHttpServer.start(JettyHttpServer.java:191)
      at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:146)
      ... 93 more
  Caused by: java.io.IOException: Failed to bind to /0.0.0.0:7070
      at wiremock.org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:346)
      at wiremock.org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:307)
      at wiremock.org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
      at wiremock.org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:231)
      at wiremock.org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
      at wiremock.org.eclipse.jetty.server.Server.doStart(Server.java:385)
      at wiremock.org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
      at com.github.tomakehurst.wiremock.jetty9.JettyHttpServer.start(JettyHttpServer.java:189)
      ... 94 more
  Caused by: java.net.BindException: Address already in use: bind
      at java.base/sun.nio.ch.Net.bind0(Native Method)
      at java.base/sun.nio.ch.Net.bind(Net.java:461)
      at java.base/sun.nio.ch.Net.bind(Net.java:453)
      at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
      at java.base/sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:80)
      at wiremock.org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:342)
      ... 101 more
   
  </code></pre>
  <p>I can not find in example project any properties, configuration or settings file.
  Please help to understand how to fix this problem.</p>