| GraphQL | REST |
|---|---|
| A query language for solving common problems when integrating APIs | An architectural style largely viewed as a conventional standard for designing APIs |
| Deployed over HTTP using a single endpoint that provides the full capabilities of the exposed service | Deployed over a set of URLs where each of them exposes a single resource |
| Uses a client-driven architecture | Uses a server-driven architecture |
| Lacks in-built caching mechanism |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class UserServiceWireMockTest { | |
| private static WireMockServer wireMockServer; | |
| private static UserService userService; | |
| @BeforeAll | |
| static void setUp() { | |
| wireMockServer = new WireMockServer(); | |
| wireMockServer.start(); | |
| WireMock.configureFor("localhost", wireMockServer.port()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependency> | |
| <groupId>com.github.tomakehurst</groupId> | |
| <artifactId>wiremock-jre8</artifactId> | |
| <version>2.32.0</version> | |
| <scope>test</scope> | |
| </dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @ExtendWith(MockitoExtension.class) | |
| class UserServiceMockitoTest { | |
| @Mock | |
| private WebClient webClient; | |
| @Mock | |
| private WebClient.RequestHeadersUriSpec requestHeadersUriSpec; | |
| @Mock | |
| private WebClient.RequestHeadersSpec requestHeadersSpec; | |
| @Mock | |
| private WebClient.ResponseSpec responseSpec; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependency> | |
| <artifactId>mockito-core</artifactId> | |
| <groupId>org.mockito</groupId> | |
| <scope>test</scope> | |
| <version>5.2.0</version> | |
| </dependency> | |
| <dependency> | |
| <artifactId>mockito-junit-jupiter</artifactId> | |
| <groupId>org.mockito</groupId> | |
| <scope>test</scope> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Test the asynchronous getUserById method | |
| @Test | |
| void whenGetUserByIdAsync_thenShouldReturnUser() throws JsonProcessingException { | |
| User expectedUser = new User(1, "Eric Cartman", "eric.cartman@email.com"); | |
| mockWebServer.enqueue(new MockResponse().setBody(objectMapper.writeValueAsString(expectedUser)) | |
| .addHeader("Content-Type", "application/json")); | |
| Mono<User> userMono = userService.getUserByIdAsync("1"); | |
| StepVerifier.create(userMono).thenAwait().expectNextMatches(user -> user.equals(expectedUser)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependency> | |
| <artifactId>mockwebserver</artifactId> | |
| <groupId>com.squareup.okhttp3</groupId> | |
| <scope>test</scope> | |
| <version>4.10.0</version> | |
| </dependency> | |
| <dependency> | |
| <artifactId>okhttp</artifactId> | |
| <groupId>com.squareup.okhttp3</groupId> | |
| <version>4.10.0</version> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UserServiceMockWebServerTest { | |
| private static MockWebServer mockWebServer; | |
| private static UserService userService; | |
| private static ObjectMapper objectMapper; | |
| // Set up the test environment by initializing the mock web server and user service | |
| @BeforeAll | |
| static void setUp() throws IOException { | |
| objectMapper = new ObjectMapper(); |
| GraphQL | REST |
|---|---|
| A query language for solving common problems when integrating APIs | An architectural style largely viewed as a conventional standard for designing APIs |
| Deployed over HTTP using a single endpoint that provides the full capabilities of the exposed service | Deployed over a set of URLs where each of them exposes a single resource |
| Uses a client-driven architecture | Uses a server-driven architecture |
| Lacks in-built caching mechanism |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CommentQueryResolver implements GraphQLQueryResolver { | |
| private ArticleRepository articleRepository; | |
| public Optional<List<Comment>> getArticleComments(final String articleId) { | |
| return articleRepository | |
| .findById(articleId) | |
| .map(Article::getComments); | |
| } | |
| } |
NewerOlder