This is an automated email from the ASF dual-hosted git repository.
shoothzj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 49ece92a5 Fixed flaky tests in ExceptionsTest.java with ObjectMapper
(#4633)
49ece92a5 is described below
commit 49ece92a576c0ab96df49a6e85d8dd9f2ad2187b
Author: Benjamin Guan <[email protected]>
AuthorDate: Thu Dec 5 02:01:06 2024 -0600
Fixed flaky tests in ExceptionsTest.java with ObjectMapper (#4633)
---
.../servicecomb/core/exception/ExceptionsTest.java | 22 ++++++++++++++++++----
1 file changed, 18 insertions(+), 4 deletions(-)
diff --git
a/core/src/test/java/org/apache/servicecomb/core/exception/ExceptionsTest.java
b/core/src/test/java/org/apache/servicecomb/core/exception/ExceptionsTest.java
index 90ffc3c94..0594084e3 100644
---
a/core/src/test/java/org/apache/servicecomb/core/exception/ExceptionsTest.java
+++
b/core/src/test/java/org/apache/servicecomb/core/exception/ExceptionsTest.java
@@ -20,6 +20,7 @@ package org.apache.servicecomb.core.exception;
import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;
import static jakarta.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static org.assertj.core.api.Assertions.assertThat;
+import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Collections;
@@ -34,6 +35,9 @@ import io.vertx.core.json.Json;
import jakarta.ws.rs.core.Response.StatusType;
class ExceptionsTest {
+
+ private static final ObjectMapper objectMapper = new ObjectMapper();
+
@Test
void should_not_convert_invocation_exception() {
InvocationException exception = Exceptions.genericConsumer("msg");
@@ -51,8 +55,13 @@ class ExceptionsTest {
assertThat(invocationException).hasCause(exception);
assertThat(invocationException.getStatus()).isEqualTo(BAD_REQUEST);
assertThat(invocationException.getErrorData()).isInstanceOf(CommonExceptionData.class);
- assertThat(Json.encode(invocationException.getErrorData()))
- .isEqualTo("{\"code\":\"SCB.00000000\",\"message\":\"Unexpected
exception when processing none. msg\"}");
+
+ try {
+
assertThat(objectMapper.readTree(Json.encode(invocationException.getErrorData())))
+
.isEqualTo(objectMapper.readTree("{\"code\":\"SCB.00000000\",\"message\":\"Unexpected
exception when processing none. msg\"}"));
+ } catch (Exception e) {
+ throw new AssertionError("Error during JSON comparison: " +
e.getMessage(), e);
+ }
}
@Test
@@ -64,8 +73,13 @@ class ExceptionsTest {
assertThat(invocationException).hasCause(exception);
assertThat(invocationException.getStatus()).isEqualTo(INTERNAL_SERVER_ERROR);
assertThat(invocationException.getErrorData()).isInstanceOf(CommonExceptionData.class);
- assertThat(Json.encode(invocationException.getErrorData()))
- .isEqualTo("{\"code\":\"SCB.50000000\",\"message\":\"Unexpected
exception when processing none. msg\"}");
+
+ try {
+
assertThat(objectMapper.readTree(Json.encode(invocationException.getErrorData())))
+
.isEqualTo(objectMapper.readTree("{\"code\":\"SCB.50000000\",\"message\":\"Unexpected
exception when processing none. msg\"}"));
+ } catch (Exception e) {
+ throw new AssertionError("Error during JSON comparison: " +
e.getMessage(), e);
+ }
}
static class ThrowExceptionWhenConvert implements
ExceptionConverter<Throwable> {