Skip to content

Instantly share code, notes, and snippets.

View stanislaw-wein's full-sized avatar
👽
.

Stanislav Vain stanislaw-wein

👽
.
View GitHub Profile
@stanislaw-wein
stanislaw-wein / UserServiceWireMockTest.java
Created April 2, 2023 03:58
UserServiceWireMockTest.java
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());
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8</artifactId>
<version>2.32.0</version>
<scope>test</scope>
</dependency>
@stanislaw-wein
stanislaw-wein / UserServiceMockitoTest.java
Last active April 2, 2023 03:55
UserServiceMockitoTest.java
@ExtendWith(MockitoExtension.class)
class UserServiceMockitoTest {
@Mock
private WebClient webClient;
@Mock
private WebClient.RequestHeadersUriSpec requestHeadersUriSpec;
@Mock
private WebClient.RequestHeadersSpec requestHeadersSpec;
@Mock
private WebClient.ResponseSpec responseSpec;
<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>
@stanislaw-wein
stanislaw-wein / UserServiceMockitoTest.java
Created April 2, 2023 03:50
UserServiceMockitoTest.java
// 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))
<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>
@stanislaw-wein
stanislaw-wein / UserServiceMockWebServerTest.java
Created April 2, 2023 03:44
UserServiceMockWebServerTest
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
@stanislaw-wein
stanislaw-wein / graphql-vs-rest.md
Created April 26, 2022 09:35
graphql-vs-rest.md
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
public class CommentQueryResolver implements GraphQLQueryResolver {
private ArticleRepository articleRepository;
public Optional<List<Comment>> getArticleComments(final String articleId) {
return articleRepository
.findById(articleId)
.map(Article::getComments);
}
}