[JAVA-10431] Use Wiremock to fix intermittent test failures
This commit is contained in:
parent
9652c01402
commit
53421889e3
@ -46,7 +46,7 @@
|
|||||||
<module>spring-cloud-circuit-breaker</module>
|
<module>spring-cloud-circuit-breaker</module>
|
||||||
<module>spring-cloud-eureka-self-preservation</module>
|
<module>spring-cloud-eureka-self-preservation</module>
|
||||||
<!-- <module>spring-cloud-openfeign</module> --> <!-- Fixing under JAVA-10446 -->
|
<!-- <module>spring-cloud-openfeign</module> --> <!-- Fixing under JAVA-10446 -->
|
||||||
<!-- <module>spring-cloud-netflix-feign</module> --> <!-- Fixing under JAVA-10431 -->
|
<module>spring-cloud-netflix-feign</module>
|
||||||
<module>spring-cloud-sentinel</module>
|
<module>spring-cloud-sentinel</module>
|
||||||
<module>spring-cloud-dapr</module>
|
<module>spring-cloud-dapr</module>
|
||||||
<module>spring-cloud-docker</module>
|
<module>spring-cloud-docker</module>
|
||||||
|
@ -51,13 +51,16 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-contract-wiremock</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<spring-cloud.version>Camden.SR7</spring-cloud.version>
|
<spring-cloud.version>Camden.SR7</spring-cloud.version>
|
||||||
<feign-ok.version>8.18.0</feign-ok.version>
|
<feign-ok.version>8.18.0</feign-ok.version>
|
||||||
<!-- <spring-cloud.version>Hoxton.SR8</spring-cloud.version> -->
|
|
||||||
<!-- <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> -->
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@FeignClient(value = "jplaceholder",
|
@FeignClient(value = "jplaceholder",
|
||||||
url = "https://jsonplaceholder.typicode.com/",
|
url = "${external.api.url}",
|
||||||
configuration = ClientConfiguration.class,
|
configuration = ClientConfiguration.class,
|
||||||
fallback = JSONPlaceHolderFallback.class)
|
fallback = JSONPlaceHolderFallback.class)
|
||||||
public interface JSONPlaceHolderClient {
|
public interface JSONPlaceHolderClient {
|
||||||
@ -19,7 +19,7 @@ public interface JSONPlaceHolderClient {
|
|||||||
@RequestMapping(method = RequestMethod.GET, value = "/posts")
|
@RequestMapping(method = RequestMethod.GET, value = "/posts")
|
||||||
List<Post> getPosts();
|
List<Post> getPosts();
|
||||||
|
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/posts/{postId}", produces = "application/json")
|
@RequestMapping(method = RequestMethod.GET, value = "/posts/{postId}", produces = "application/json")
|
||||||
Post getPostById(@PathVariable("postId") Long postId);
|
Post getPostById(@PathVariable("postId") Long postId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
spring.application.name=netflix-feign
|
spring.application.name=netflix-feign
|
||||||
logging.level.com.baeldung.cloud.netflix.feign.client=DEBUG
|
logging.level.com.baeldung.cloud.netflix.feign.client=DEBUG
|
||||||
feign.hystrix.enabled=true
|
feign.hystrix.enabled=true
|
||||||
|
|
||||||
|
external.api.url=https://jsonplaceholder.typicode.com/
|
@ -1,21 +0,0 @@
|
|||||||
package com.baeldung.cloud.netflix.feign;
|
|
||||||
|
|
||||||
import com.baeldung.cloud.netflix.feign.config.ClientConfiguration;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
|
||||||
@SpringBootTest
|
|
||||||
@EnableAutoConfiguration
|
|
||||||
@ContextConfiguration(classes = { ClientConfiguration.class })
|
|
||||||
public class ExampleTestApplication {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,42 +2,97 @@ package com.baeldung.cloud.netflix.feign;
|
|||||||
|
|
||||||
import com.baeldung.cloud.netflix.feign.model.Post;
|
import com.baeldung.cloud.netflix.feign.model.Post;
|
||||||
import com.baeldung.cloud.netflix.feign.service.JSONPlaceHolderService;
|
import com.baeldung.cloud.netflix.feign.service.JSONPlaceHolderService;
|
||||||
|
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
|
||||||
|
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||||
|
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
|
||||||
|
import org.junit.Before;
|
||||||
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;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||||
|
import static com.github.tomakehurst.wiremock.client.WireMock.exactly;
|
||||||
|
import static com.github.tomakehurst.wiremock.client.WireMock.get;
|
||||||
|
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
|
||||||
|
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
|
||||||
|
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest(properties = {"external.api.url=http://localhost:${wiremock.server.port}"})
|
||||||
|
@AutoConfigureWireMock(port = 0)
|
||||||
public class NetflixFeignUnitTest {
|
public class NetflixFeignUnitTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private JSONPlaceHolderService jsonPlaceHolderService;
|
private JSONPlaceHolderService jsonPlaceHolderService;
|
||||||
|
|
||||||
@Test
|
@Before
|
||||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
public void setup() {
|
||||||
|
WireMock.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetPosts_thenListPostSizeGreaterThanZero() {
|
public void givenExternalApiAvailable_whenGetPosts_thenPostsReturned() {
|
||||||
|
|
||||||
|
WireMock.stubFor(get(urlEqualTo("/posts"))
|
||||||
|
.willReturn(okJson("[{ \"userId\": 1, \"id\": 1, \"title\": \"post 1 title\", \"body\": \"post 1 body\" }, "
|
||||||
|
+ "{ \"userId\": 1, \"id\": 2, \"title\": \"post 2 title\", \"body\": \"post 2 body\" }]")));
|
||||||
|
|
||||||
List<Post> posts = jsonPlaceHolderService.getPosts();
|
List<Post> posts = jsonPlaceHolderService.getPosts();
|
||||||
|
|
||||||
assertFalse(posts.isEmpty());
|
assertEquals(2, posts.size());
|
||||||
|
verify(exactly(1), getRequestedFor(urlEqualTo("/posts")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetPostWithId_thenPostExist() {
|
public void givenExternalApiUnavailable_whenGetPosts_thenEmpty() {
|
||||||
|
|
||||||
|
WireMock.stubFor(get(urlEqualTo("/posts"))
|
||||||
|
.willReturn(aResponse().withStatus(500)));
|
||||||
|
|
||||||
|
List<Post> posts = jsonPlaceHolderService.getPosts();
|
||||||
|
|
||||||
|
assertTrue(posts.isEmpty());
|
||||||
|
verify(exactly(1), getRequestedFor(urlEqualTo("/posts")));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenExternalApiAvailable_whenGetPostWithId_thenPostExists() {
|
||||||
|
|
||||||
|
WireMock.stubFor(get(urlEqualTo("/posts/1"))
|
||||||
|
.willReturn(okJson("{ \"userId\": 1, \"id\": 1, \"title\": \"post 1 title\", \"body\": \"post 1 body\" }")));
|
||||||
|
|
||||||
Post post = jsonPlaceHolderService.getPostById(1L);
|
Post post = jsonPlaceHolderService.getPostById(1L);
|
||||||
|
|
||||||
assertNotNull(post);
|
assertNotNull(post);
|
||||||
|
verify(exactly(1), getRequestedFor(urlEqualTo("/posts/1")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenExternalApiUnavailable_whenGetPostWithId_thenNull() {
|
||||||
|
|
||||||
|
WireMock.stubFor(get(urlEqualTo("/posts/1"))
|
||||||
|
.willReturn(aResponse().withStatus(500)));
|
||||||
|
|
||||||
|
Post post = jsonPlaceHolderService.getPostById(1L);
|
||||||
|
|
||||||
|
assertNull(post);
|
||||||
|
verify(exactly(1), getRequestedFor(urlEqualTo("/posts/1")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ResponseDefinitionBuilder okJson(String json) {
|
||||||
|
return aResponse()
|
||||||
|
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
|
||||||
|
.withBody(json);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user