Skip to content

Commit 8f70ae3

Browse files
committed
test: refactor RedisConnectionFactoryTest to improve invalid node assertions
1 parent 263fad4 commit 8f70ae3

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

shenyu-infra/shenyu-infra-redis/src/test/java/org/apache/shenyu/infra/redis/RedisConnectionFactoryTest.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.time.Duration;
2727
import java.lang.reflect.Method;
28+
import java.lang.reflect.InvocationTargetException;
2829

2930
import static org.mockito.Mockito.when;
3031

@@ -90,14 +91,10 @@ public void parseRedisNodeInvalidInputs() throws Exception {
9091
parseMethod.setAccessible(true);
9192

9293
Assertions.assertAll(
93-
() -> Assertions.assertThrows(IllegalArgumentException.class,
94-
() -> parseMethod.invoke(factory, "")),
95-
() -> Assertions.assertThrows(IllegalArgumentException.class,
96-
() -> parseMethod.invoke(factory, "[::1]:")),
97-
() -> Assertions.assertThrows(IllegalArgumentException.class,
98-
() -> parseMethod.invoke(factory, "localhost:70000")),
99-
() -> Assertions.assertThrows(IllegalArgumentException.class,
100-
() -> parseMethod.invoke(factory, "[]:6379"))
94+
() -> assertInvalidNode(parseMethod, factory, ""),
95+
() -> assertInvalidNode(parseMethod, factory, "[::1]:"),
96+
() -> assertInvalidNode(parseMethod, factory, "localhost:70000"),
97+
() -> assertInvalidNode(parseMethod, factory, "[]:6379")
10198
);
10299
}
103100

@@ -107,4 +104,18 @@ private RedisConnectionFactory createFactoryWithDefaultUrl() {
107104
redisConfigProperties.setMode(RedisModeEnum.STANDALONE.getName());
108105
return new RedisConnectionFactory(redisConfigProperties);
109106
}
107+
108+
private void assertInvalidNode(final Method parseMethod, final RedisConnectionFactory factory, final String url) {
109+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
110+
try {
111+
parseMethod.invoke(factory, url);
112+
} catch (InvocationTargetException e) {
113+
// unwrap to the original IllegalArgumentException
114+
if (e.getCause() instanceof RuntimeException) {
115+
throw (RuntimeException) e.getCause();
116+
}
117+
throw new RuntimeException(e.getCause());
118+
}
119+
});
120+
}
110121
}

0 commit comments

Comments
 (0)