Skip to content

Commit

Permalink
Refactor assertions to use assertThat with appropriate matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
abdelhak-zaaim committed Nov 15, 2024
1 parent 86122fc commit 8704264
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class InterceptorRuntimeTest {
Expand Down Expand Up @@ -91,9 +90,10 @@ void createNoArgBasedInterceptorSource() throws Exception {
assertThat(file.exists(), is(true));
String java = Files.readString(file.toPath());
String expected = loadStringFromResource("expected/ximpl-interceptor._java_");
assertEquals(
expected.replaceFirst("#DATE#", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))),
java);
assertThat(
java,
is(expected.replaceFirst("#DATE#", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))))
);
}

@Test
Expand All @@ -104,9 +104,10 @@ void createInterfaceBasedInterceptorSource() throws Exception {
assertThat(file.exists(), is(true));
String java = Files.readString(file.toPath());
String expected = loadStringFromResource("expected/yimpl-interceptor._java_");
assertEquals(
expected.replaceFirst("#DATE#", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))),
java);
assertThat(
java,
is(expected.replaceFirst("#DATE#", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))))
);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
* Copyright (c) 2023, 2024 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 @@ -28,7 +28,6 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
Expand Down Expand Up @@ -67,7 +66,7 @@ void codegenHelloActivator() {
.build();

ToolsException te = assertThrows(ToolsException.class, () -> activatorCreator.createModuleActivators(req));
assertEquals("Failed in create", te.getMessage());
assertThat(te.getMessage(), is("Failed in create"));

ActivatorCreatorRequest req2 = ActivatorCreatorRequest.builder()
.serviceTypeNames(Collections.singletonList(TypeName.create(HelloInjectionWorldImpl.class)))
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, 2024 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 @@ -28,7 +28,8 @@
import org.junit.jupiter.api.Test;
import org.opentest4j.AssertionFailedError;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.jupiter.api.Assertions.fail;

public class InvalidStateTest {
Expand Down Expand Up @@ -124,7 +125,7 @@ void assertDeploymentException(Class<? extends Throwable> expected, Map<String,
} catch (AssertionFailedError e) {
throw e;
} catch (Throwable e) {
assertEquals(expected, e.getClass());
assertThat(e, instanceOf(expected));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
* Copyright (c) 2021, 2024 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 @@ -27,7 +27,6 @@

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

@HelidonTest
class SecretTest {
Expand All @@ -43,6 +42,6 @@ public void testSecrets() {
.get();
assertThat(r.getStatus(), is(Response.Status.OK.getStatusCode()));
JsonObject o = r.readEntity(JsonObject.class);
assertEquals(o.get("secret1"), o.get("secret2"));
assertThat(o.get("secret1"), is(o.get("secret2")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import static io.helidon.http.Method.GET;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -90,7 +89,7 @@ void testDefaultIsSystem1() {
try {
ProxySelector.setDefault(ProxySelector.of(new InetSocketAddress(PROXY_HOST, proxyPort)));
Proxy proxy = Proxy.builder().build();
assertEquals(ProxyType.SYSTEM, proxy.type());
assertThat(proxy.type(), is(ProxyType.SYSTEM));
successVerify(proxy, clientHttp1);
} finally {
ProxySelector.setDefault(original);
Expand All @@ -103,7 +102,7 @@ void testDefaultIsSystem2() {
try {
ProxySelector.setDefault(ProxySelector.of(new InetSocketAddress(PROXY_HOST, proxyPort)));
Proxy proxy = Proxy.builder().build();
assertEquals(ProxyType.SYSTEM, proxy.type());
assertThat(proxy.type(), is(ProxyType.SYSTEM));
successVerify(proxy, clientHttp2);
} finally {
ProxySelector.setDefault(original);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
* Copyright (c) 2022, 2024 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 @@ -53,7 +53,6 @@
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

@ServerTest
Expand Down Expand Up @@ -141,15 +140,15 @@ static void router(HttpRouting.Builder router) {
void testOk() {
var response = request("/");

assertEquals(200, response.statusCode());
assertThat(response.statusCode(), is(200));
assertThat(response.body(), is("ok"));
}

@Test
void testGetOutputStreamThenError_expect_CustomErrorHandlerMessage() {
var response = request("/get-outputStream");

assertEquals(418, response.statusCode());
assertThat(response.statusCode(), is(418));
assertThat(response.body(), is("TeaPotIAm"));
assertThat(response.headers().firstValue(ERROR_HEADER_NAME.lowerCase()), is(Optional.of("err")));
assertThat(response.headers().firstValue(MAIN_HEADER_NAME.lowerCase()), is(emptyOptional()));
Expand All @@ -159,7 +158,7 @@ void testGetOutputStreamThenError_expect_CustomErrorHandlerMessage() {
void testGetOutputStreamWriteOnceThenError_expect_CustomErrorHandlerMessage() {
var response = request("/get-outputStream-writeOnceThenError");

assertEquals(418, response.statusCode());
assertThat(response.statusCode(), is(418));
assertThat(response.body(), is("TeaPotIAm"));
assertThat(response.headers().firstValue(ERROR_HEADER_NAME.lowerCase()), is(Optional.of("err")));
assertThat(response.headers().firstValue(MAIN_HEADER_NAME.lowerCase()), is(emptyOptional()));
Expand All @@ -185,7 +184,7 @@ void testGetOutputStreamWriteFlushThenError_expect_invalidResponse() {
void testGetOutputStreamTryWithResourcesThenError_expect_CustomErrorHandlerMessage() {
var response = request("/get-outputStream-tryWithResources");

assertEquals(418, response.statusCode());
assertThat(response.statusCode(), is(418));
assertThat(response.body(), is("TeaPotIAm"));
assertThat(response.headers().firstValue(ERROR_HEADER_NAME.lowerCase()), is(Optional.of("err")));
assertThat(response.headers().firstValue(MAIN_HEADER_NAME.lowerCase()), is(emptyOptional()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -138,7 +137,7 @@ public void testCloseConnectionExceptionContainsCause() {
});
fail("It is expected a CloseConnectionException");
} catch (CloseConnectionException e) {
assertEquals(OtherException.class, e.getCause().getClass());
assertThat(e.getCause(), instanceOf(OtherException.class));
}
}

Expand Down

0 comments on commit 8704264

Please sign in to comment.