This repository was archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokemonControllerIntegrationTest.java
More file actions
74 lines (60 loc) · 2.67 KB
/
Copy pathPokemonControllerIntegrationTest.java
File metadata and controls
74 lines (60 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.origin.challenge.controllers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
@SuppressWarnings({"unchecked", "rawtypes"})
public class PokemonControllerIntegrationTest {
@Mock
private HttpClient httpClient;
@InjectMocks
private PokemonController pokemonController;
private static HttpResponse httpResponse;
@BeforeEach
public void setup() {
httpResponse = mock(HttpResponse.class);
when(httpResponse.body()).thenReturn("{\"count\":1154,\"next\":\"https://pokeapi.co/api/v2/pokemon?offset=20" +
"&limit=20\",\"previous\":null,\"results\":[{\"name\":\"bulbasaur\",\"url\":\"https://pokeapi.co/api" +
"/v2/pokemon/1/\"}]}"
);
}
@Test
void test1() throws Exception {
when(httpClient.send(any(), any())).thenReturn(httpResponse);
final HttpRequest expected = HttpRequest.newBuilder()
.uri(new URI("https://pokeapi.co/api/v2/pokemon?offset=0&limit=20"))
.header(
"Authorization",
"eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZ" +
"SIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI"
)
.GET()
.build();
pokemonController.listPokemon(1);
verify(httpClient, times(1)).send(expected, HttpResponse.BodyHandlers.ofString());
}
@Test
void test2() throws Exception {
when(httpClient.send(any(), any())).thenReturn(httpResponse);
final HttpRequest expected = HttpRequest.newBuilder()
.uri(new URI("https://pokeapi.co/api/v2/pokemon?offset=20&limit=20"))
.header(
"Authorization",
"eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iLCJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZ" +
"SIsImV4cCI6MTY2MjA0MjMzNCwiaWF0IjoxNjYyMDQyMzM0fQ.xi3uKpbHXXxE5iTOkDrkHJfpXQhGQGjLHXwC1SE-kFI"
)
.GET()
.build();
pokemonController.listPokemon(2);
verify(httpClient, times(1)).send(expected, HttpResponse.BodyHandlers.ofString());
}
}