Untitled
supelpawel
javascript
3 years ago
1.7 kB
4
Indexable
// Klasa z metodą odpowiadającą za pobieranie danych z endpointa public class Posts { private static final String POSTS_API_URL = "https://jsonplaceholder.typicode.com/posts"; public static List<PostDto> getPosts() throws IOException, InterruptedException { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .GET() .header("accept", "application/json") .uri(URI.create(POSTS_API_URL)) .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); ObjectMapper mapper = new ObjectMapper(); List<PostDto> posts = mapper.readValue(response.body(), new TypeReference<>() { }); if (posts != null || posts.size() > 0) { return posts; } else { throw new IOException("No posts"); } } public static void main(String[] args) throws IOException, InterruptedException { System.out.println(getPosts()); } } // testy import org.junit.jupiter.api.Test; import java.io.IOException; import static org.junit.jupiter.api.Assertions.assertEquals; class PostsTest { @Test void getPosts1() throws IOException, InterruptedException { Posts posts = new Posts(); boolean expected = true; boolean actual = Posts.getPosts().size() > 0; assertEquals(expected, actual); } @Test void getPosts2() throws IOException, InterruptedException { Posts posts = new Posts(); boolean expected = true; boolean actual = Posts.getPosts() != null; assertEquals(expected, actual); } }
Editor is loading...