Merge pull request #15077 from ehsansasanian/java-25346-fix-spring-cloud-contract-consumer

Java-24346: Fix integration test for spring-cloud-contract-consumer m…
This commit is contained in:
Kasra Madadipouya 2023-10-28 20:49:16 +02:00 committed by GitHub
commit a1698c6b83
1 changed files with 36 additions and 0 deletions

View File

@ -1,5 +1,10 @@
package com.baeldung.spring.cloud.springcloudcontractconsumer.controller; package com.baeldung.spring.cloud.springcloudcontractconsumer.controller;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -13,6 +18,7 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -26,6 +32,36 @@ public class BasicMathControllerIntegrationTest {
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
private static WireMockServer wireMockServer;
@BeforeClass
public static void setupClass() {
WireMockConfiguration wireMockConfiguration = WireMockConfiguration.options().port(8090); // Use the same port as in your code
wireMockServer = new WireMockServer(wireMockConfiguration);
wireMockServer.start();
}
@AfterClass
public static void teardownClass() {
wireMockServer.stop();
}
@Before
public void setup() {
wireMockServer.stubFor(get(urlEqualTo("/validate/prime-number?number=1"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("Odd")));
wireMockServer.stubFor(get(urlEqualTo("/validate/prime-number?number=2"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("Even")));
}
@Test @Test
public void given_WhenPassEvenNumberInQueryParam_ThenReturnEven() throws Exception { public void given_WhenPassEvenNumberInQueryParam_ThenReturnEven() throws Exception {