Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x]: Use Hamcrest assertions instead of JUnit in examples/todo-app (#1749) #6292

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,7 +42,8 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@HelidonTest
@Configuration(useExisting = true)
Expand Down Expand Up @@ -115,16 +116,16 @@ void testTodoScenario() {
.header(Http.Header.AUTHORIZATION, basicAuth)
.post(Entity.json(todo), JsonObject.class);

assertEquals("john", returnedTodo.getString("user"));
assertEquals(todo.getString("title"), returnedTodo.getString("title"));
assertThat(returnedTodo.getString("user"), is("john"));
assertThat(returnedTodo.getString("title"), is(todo.getString("title")));

// Get the todo created earlier
JsonObject fromServer = webTarget.path("/api/backend/" + returnedTodo.getString("id"))
.request(MediaType.APPLICATION_JSON_TYPE)
.header(Http.Header.AUTHORIZATION, basicAuth)
.get(JsonObject.class);

assertEquals(returnedTodo, fromServer);
assertThat(fromServer, is(returnedTodo));

// Update the todo created earlier
JsonObject updatedTodo = Json.createObjectBuilder()
Expand All @@ -137,23 +138,23 @@ void testTodoScenario() {
.header(Http.Header.AUTHORIZATION, basicAuth)
.put(Entity.json(updatedTodo), JsonObject.class);

assertEquals(updatedTodo.getString("title"), fromServer.getString("title"));
assertThat(fromServer.getString("title"), is(updatedTodo.getString("title")));

// Delete the todo created earlier
fromServer = webTarget.path("/api/backend/" + returnedTodo.getString("id"))
.request(MediaType.APPLICATION_JSON_TYPE)
.header(Http.Header.AUTHORIZATION, basicAuth)
.delete(JsonObject.class);

assertEquals(returnedTodo.getString("id"), fromServer.getString("id"));
assertThat(fromServer.getString("id"), is(returnedTodo.getString("id")));

// Get list of todos
JsonArray jsonValues = webTarget.path("/api/backend")
.request(MediaType.APPLICATION_JSON_TYPE)
.header(Http.Header.AUTHORIZATION, basicAuth)
.get(JsonArray.class);

assertEquals(0, jsonValues.size(), "There should be no todos on server");
assertThat("There should be no todos on server", jsonValues.size(), is(0));
}

}
5 changes: 5 additions & 0 deletions examples/todo-app/frontend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-jersey</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 Oracle and/or its affiliates.
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -48,12 +48,14 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static io.helidon.config.ConfigSources.classpath;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

public class FrontendTest {

private static WebServer serverBackend;
Expand Down Expand Up @@ -184,7 +186,7 @@ public void testGetList() throws ExecutionException, InterruptedException {
})
.request(JsonArray.class)
.thenAccept(jsonValues -> {
Assertions.assertEquals(TODO, jsonValues.getJsonObject(0));
assertThat(jsonValues.getJsonObject(0), is(TODO));
})
.toCompletableFuture()
.get();
Expand All @@ -200,7 +202,7 @@ public void testPostTodo() throws ExecutionException, InterruptedException {
})
.submit(TODO, JsonObject.class)
.thenAccept(jsonObject -> {
Assertions.assertEquals(TODO, jsonObject);
assertThat(jsonObject, is(TODO));
})
.toCompletableFuture()
.get();
Expand All @@ -216,7 +218,7 @@ public void testGetTodo() throws ExecutionException, InterruptedException {
})
.request(JsonObject.class)
.thenAccept(jsonObject -> {
Assertions.assertEquals(TODO, jsonObject);
assertThat(jsonObject, is(TODO));
})
.toCompletableFuture()
.get();
Expand All @@ -232,7 +234,7 @@ public void testDeleteTodo() throws ExecutionException, InterruptedException {
})
.request(JsonObject.class)
.thenAccept(jsonObject -> {
Assertions.assertEquals(TODO, jsonObject);
assertThat(jsonObject, is(TODO));
})
.toCompletableFuture()
.get();
Expand All @@ -248,7 +250,7 @@ public void testUpdateTodo() throws ExecutionException, InterruptedException {
})
.submit(TODO, JsonObject.class)
.thenAccept(jsonObject -> {
Assertions.assertEquals(TODO, jsonObject);
assertThat(jsonObject, is(TODO));
})
.toCompletableFuture()
.get();
Expand All @@ -260,7 +262,7 @@ public void testEnvHandler() throws ExecutionException, InterruptedException {
.path("/env")
.request(String.class)
.thenAccept(s -> {
Assertions.assertEquals("docker", s);
assertThat(s, is("docker"));
})
.toCompletableFuture()
.get();
Expand Down