Skip to content

Commit a5868d6

Browse files
Refactor assertions to use assertThat with appropriate matchers (helidon-io#9503)
1 parent 7f9d8ac commit a5868d6

7 files changed

Lines changed: 24 additions & 27 deletions

File tree

inject/tests/resources-inject/src/test/java/io/helidon/inject/tests/inject/interceptor/InterceptorRuntimeTest.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
import static org.hamcrest.CoreMatchers.is;
6060
import static org.hamcrest.MatcherAssert.assertThat;
6161
import static org.hamcrest.Matchers.contains;
62-
import static org.junit.jupiter.api.Assertions.assertEquals;
6362
import static org.junit.jupiter.api.Assertions.assertThrows;
6463

6564
class InterceptorRuntimeTest {
@@ -91,9 +90,10 @@ void createNoArgBasedInterceptorSource() throws Exception {
9190
assertThat(file.exists(), is(true));
9291
String java = Files.readString(file.toPath());
9392
String expected = loadStringFromResource("expected/ximpl-interceptor._java_");
94-
assertEquals(
95-
expected.replaceFirst("#DATE#", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))),
96-
java);
93+
assertThat(
94+
java,
95+
is(expected.replaceFirst("#DATE#", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))))
96+
);
9797
}
9898

9999
@Test
@@ -104,9 +104,10 @@ void createInterfaceBasedInterceptorSource() throws Exception {
104104
assertThat(file.exists(), is(true));
105105
String java = Files.readString(file.toPath());
106106
String expected = loadStringFromResource("expected/yimpl-interceptor._java_");
107-
assertEquals(
108-
expected.replaceFirst("#DATE#", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))),
109-
java);
107+
assertThat(
108+
java,
109+
is(expected.replaceFirst("#DATE#", Integer.toString(Calendar.getInstance().get(Calendar.YEAR))))
110+
);
110111
}
111112

112113
@Test

inject/tools/src/test/java/io/helidon/inject/tools/ActivatorCreatorDefaultTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2023, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,6 @@
2828
import static org.hamcrest.CoreMatchers.equalTo;
2929
import static org.hamcrest.CoreMatchers.is;
3030
import static org.hamcrest.MatcherAssert.assertThat;
31-
import static org.junit.jupiter.api.Assertions.assertEquals;
3231
import static org.junit.jupiter.api.Assertions.assertThrows;
3332

3433
/**
@@ -67,7 +66,7 @@ void codegenHelloActivator() {
6766
.build();
6867

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

7271
ActivatorCreatorRequest req2 = ActivatorCreatorRequest.builder()
7372
.serviceTypeNames(Collections.singletonList(TypeName.create(HelloInjectionWorldImpl.class)))

microprofile/scheduling/src/test/java/io/helidon/microprofile/scheduling/InvalidStateTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021 Oracle and/or its affiliates.
2+
* Copyright (c) 2021, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,8 @@
2828
import org.junit.jupiter.api.Test;
2929
import org.opentest4j.AssertionFailedError;
3030

31-
import static org.junit.jupiter.api.Assertions.assertEquals;
31+
import static org.hamcrest.MatcherAssert.assertThat;
32+
import static org.hamcrest.Matchers.instanceOf;
3233
import static org.junit.jupiter.api.Assertions.fail;
3334

3435
public class InvalidStateTest {
@@ -124,7 +125,7 @@ void assertDeploymentException(Class<? extends Throwable> expected, Map<String,
124125
} catch (AssertionFailedError e) {
125126
throw e;
126127
} catch (Throwable e) {
127-
assertEquals(expected, e.getClass());
128+
assertThat(e, instanceOf(expected));
128129
}
129130
}
130131
}

tests/functional/request-scope-cdi/src/test/java/io/helidon/tests/functional/requestscopecdi/SecretTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2021, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,6 @@
2727

2828
import static org.hamcrest.CoreMatchers.is;
2929
import static org.hamcrest.MatcherAssert.assertThat;
30-
import static org.junit.jupiter.api.Assertions.assertEquals;
3130

3231
@HelidonTest
3332
class SecretTest {
@@ -43,6 +42,6 @@ public void testSecrets() {
4342
.get();
4443
assertThat(r.getStatus(), is(Response.Status.OK.getStatusCode()));
4544
JsonObject o = r.readEntity(JsonObject.class);
46-
assertEquals(o.get("secret1"), o.get("secret2"));
45+
assertThat(o.get("secret1"), is(o.get("secret2")));
4746
}
4847
}

webclient/tests/http1/src/test/java/io/helidon/webclient/tests/HttpProxyTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import static io.helidon.http.Method.GET;
4141
import static org.hamcrest.CoreMatchers.is;
4242
import static org.hamcrest.MatcherAssert.assertThat;
43-
import static org.junit.jupiter.api.Assertions.assertEquals;
4443
import static org.junit.jupiter.api.Assertions.assertFalse;
4544
import static org.junit.jupiter.api.Assertions.assertTrue;
4645

@@ -90,7 +89,7 @@ void testDefaultIsSystem1() {
9089
try {
9190
ProxySelector.setDefault(ProxySelector.of(new InetSocketAddress(PROXY_HOST, proxyPort)));
9291
Proxy proxy = Proxy.builder().build();
93-
assertEquals(ProxyType.SYSTEM, proxy.type());
92+
assertThat(proxy.type(), is(ProxyType.SYSTEM));
9493
successVerify(proxy, clientHttp1);
9594
} finally {
9695
ProxySelector.setDefault(original);
@@ -103,7 +102,7 @@ void testDefaultIsSystem2() {
103102
try {
104103
ProxySelector.setDefault(ProxySelector.of(new InetSocketAddress(PROXY_HOST, proxyPort)));
105104
Proxy proxy = Proxy.builder().build();
106-
assertEquals(ProxyType.SYSTEM, proxy.type());
105+
assertThat(proxy.type(), is(ProxyType.SYSTEM));
107106
successVerify(proxy, clientHttp2);
108107
} finally {
109108
ProxySelector.setDefault(original);

webserver/tests/http2/src/test/java/io/helidon/webserver/tests/http2/Http2ErrorHandlingWithOutputStreamTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
2+
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,7 +53,6 @@
5353
import static org.hamcrest.CoreMatchers.instanceOf;
5454
import static org.hamcrest.CoreMatchers.is;
5555
import static org.hamcrest.MatcherAssert.assertThat;
56-
import static org.junit.jupiter.api.Assertions.assertEquals;
5756
import static org.junit.jupiter.api.Assertions.assertThrows;
5857

5958
@ServerTest
@@ -141,15 +140,15 @@ static void router(HttpRouting.Builder router) {
141140
void testOk() {
142141
var response = request("/");
143142

144-
assertEquals(200, response.statusCode());
143+
assertThat(response.statusCode(), is(200));
145144
assertThat(response.body(), is("ok"));
146145
}
147146

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

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

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

188-
assertEquals(418, response.statusCode());
187+
assertThat(response.statusCode(), is(418));
189188
assertThat(response.body(), is("TeaPotIAm"));
190189
assertThat(response.headers().firstValue(ERROR_HEADER_NAME.lowerCase()), is(Optional.of("err")));
191190
assertThat(response.headers().firstValue(MAIN_HEADER_NAME.lowerCase()), is(emptyOptional()));

webserver/webserver/src/test/java/io/helidon/webserver/http/ErrorHandlersTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444
import static org.hamcrest.CoreMatchers.is;
4545
import static org.hamcrest.MatcherAssert.assertThat;
4646
import static org.junit.jupiter.api.Assertions.assertAll;
47-
import static org.junit.jupiter.api.Assertions.assertEquals;
4847
import static org.junit.jupiter.api.Assertions.fail;
4948
import static org.mockito.Mockito.mock;
5049
import static org.mockito.Mockito.verify;
@@ -138,7 +137,7 @@ public void testCloseConnectionExceptionContainsCause() {
138137
});
139138
fail("It is expected a CloseConnectionException");
140139
} catch (CloseConnectionException e) {
141-
assertEquals(OtherException.class, e.getCause().getClass());
140+
assertThat(e.getCause(), instanceOf(OtherException.class));
142141
}
143142
}
144143

0 commit comments

Comments
 (0)