Skip to content

Commit

Permalink
[JAVA-10431] Use Wiremock to fix intermittent test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
hkhan committed Apr 4, 2022
1 parent 84811d8 commit a3fe154
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 32 deletions.
2 changes: 1 addition & 1 deletion spring-cloud/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<module>spring-cloud-circuit-breaker</module>
<module>spring-cloud-eureka-self-preservation</module>
<!-- <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-dapr</module>
<module>spring-cloud-docker</module>
Expand Down
7 changes: 5 additions & 2 deletions spring-cloud/spring-cloud-netflix-feign/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,16 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-wiremock</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<spring-cloud.version>Camden.SR7</spring-cloud.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>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import java.util.List;

@FeignClient(value = "jplaceholder",
url = "https://jsonplaceholder.typicode.com/",
url = "${external.api.url}",
configuration = ClientConfiguration.class,
fallback = JSONPlaceHolderFallback.class)
public interface JSONPlaceHolderClient {

@RequestMapping(method = RequestMethod.GET, value = "/posts")
List<Post> getPosts();


@RequestMapping(method = RequestMethod.GET, value = "/posts/{postId}", produces = "application/json")
Post getPostById(@PathVariable("postId") Long postId);

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
spring.application.name=netflix-feign
logging.level.com.baeldung.cloud.netflix.feign.client=DEBUG
feign.hystrix.enabled=true

external.api.url=https://jsonplaceholder.typicode.com/

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,97 @@

import com.baeldung.cloud.netflix.feign.model.Post;
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.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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 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.assertNull;
import static org.junit.Assert.assertTrue;

@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootTest(properties = {"external.api.url=http://localhost:${wiremock.server.port}"})
@AutoConfigureWireMock(port = 0)
public class NetflixFeignUnitTest {

@Autowired
private JSONPlaceHolderService jsonPlaceHolderService;

@Before
public void setup() {
WireMock.reset();
}

@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
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();

assertEquals(2, posts.size());
verify(exactly(1), getRequestedFor(urlEqualTo("/posts")));
}

@Test
public void whenGetPosts_thenListPostSizeGreaterThanZero() {
public void givenExternalApiUnavailable_whenGetPosts_thenEmpty() {

WireMock.stubFor(get(urlEqualTo("/posts"))
.willReturn(aResponse().withStatus(500)));

List<Post> posts = jsonPlaceHolderService.getPosts();

assertFalse(posts.isEmpty());
assertTrue(posts.isEmpty());
verify(exactly(1), getRequestedFor(urlEqualTo("/posts")));
}

@Test
public void whenGetPostWithId_thenPostExist() {
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);

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);
}
}

0 comments on commit a3fe154

Please sign in to comment.