diff --git a/shenyu-register-center/shenyu-register-client-beat/src/test/java/org/apache/shenyu/register/client/beat/HeartbeatListenerTest.java b/shenyu-register-center/shenyu-register-client-beat/src/test/java/org/apache/shenyu/register/client/beat/HeartbeatListenerTest.java index ea0cd06e87d3..17929133f8d3 100644 --- a/shenyu-register-center/shenyu-register-client-beat/src/test/java/org/apache/shenyu/register/client/beat/HeartbeatListenerTest.java +++ b/shenyu-register-center/shenyu-register-client-beat/src/test/java/org/apache/shenyu/register/client/beat/HeartbeatListenerTest.java @@ -29,13 +29,16 @@ import org.springframework.boot.autoconfigure.web.ServerProperties; import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; import java.util.Optional; import java.util.Properties; import java.util.concurrent.ScheduledThreadPoolExecutor; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; @@ -183,12 +186,9 @@ void testSendHeartbeatWithLoginFailure() throws Exception { org.apache.shenyu.register.common.dto.InstanceBeatInfoDTO beatInfo = new org.apache.shenyu.register.common.dto.InstanceBeatInfoDTO(); - // Should throw RuntimeException due to login failure - try { - sendHeartbeatMethod.invoke(heartbeatListener, beatInfo); - } catch (Exception e) { - assertTrue(e.getCause() instanceof RuntimeException); - } + InvocationTargetException exception = assertThrows(InvocationTargetException.class, + () -> sendHeartbeatMethod.invoke(heartbeatListener, beatInfo)); + assertInstanceOf(RuntimeException.class, exception.getCause()); } }