Skip to content

Commit 5358b19

Browse files
committed
Polishing.
Use parametrized tests for Command enum verification. See #3191 Original pull request: #3193
1 parent 3a930ce commit 5358b19

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

src/test/java/org/springframework/data/redis/core/RedisCommandUnitTests.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20-
import java.util.Arrays;
21-
2220
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.EnumSource;
23+
2324
import org.springframework.test.util.ReflectionTestUtils;
2425

2526
/**
@@ -106,37 +107,34 @@ void shouldThrowExceptionOnInvalidArgumentCountForZaddWhenExpectedMinimalMatch()
106107
.withMessageContaining("ZADD command requires at least 3 arguments");
107108
}
108109

109-
@Test // GH-2644
110-
void isRepresentedByIsCorrectForAllCommandsAndTheirAliases() {
111-
112-
for (RedisCommand command : RedisCommand.values()) {
110+
@ParameterizedTest(name = "{0}") // GH-2644
111+
@EnumSource(RedisCommand.class)
112+
void isRepresentedByIsCorrectForAllCommandsAndTheirAliases(RedisCommand command) {
113113

114-
assertThat(command.isRepresentedBy(command.name())).isTrue();
115-
assertThat(command.isRepresentedBy(command.name().toLowerCase())).isTrue();
114+
assertThat(command.isRepresentedBy(command.name())).isTrue();
115+
assertThat(command.isRepresentedBy(command.name().toLowerCase())).isTrue();
116116

117-
for (String alias : command.getAliases()) {
118-
assertThat(command.isRepresentedBy(alias)).isTrue();
119-
assertThat(command.isRepresentedBy(alias.toUpperCase())).isTrue();
120-
}
117+
for (String alias : command.getAliases()) {
118+
assertThat(command.isRepresentedBy(alias)).isTrue();
119+
assertThat(command.isRepresentedBy(alias.toUpperCase())).isTrue();
121120
}
122121
}
123122

124-
@Test // GH-2646
125-
void commandRequiresArgumentsIsCorrect() {
123+
@ParameterizedTest(name = "{0}") // GH-2646
124+
@EnumSource(RedisCommand.class)
125+
void commandRequiresArgumentsIsCorrect(RedisCommand command) {
126126

127-
Arrays.stream(RedisCommand.values())
128-
.forEach(command -> assertThat(command.requiresArguments())
129-
.describedAs("Redis command [%s] failed required arguments check", command)
130-
.isEqualTo((int) ReflectionTestUtils.getField(command, "minArgs") > 0));
127+
assertThat(command.requiresArguments()).describedAs("Redis command [%s] failed required arguments check", command)
128+
.isEqualTo((int) ReflectionTestUtils.getField(command, "minArgs") > 0);
131129
}
132130

133-
@Test // GH-2646
134-
void commandRequiresExactNumberOfArgumentsIsCorrect() {
131+
@ParameterizedTest(name = "{0}") // GH-2646
132+
@EnumSource(RedisCommand.class)
133+
void commandRequiresExactNumberOfArgumentsIsCorrect(RedisCommand command) {
135134

136-
Arrays.stream(RedisCommand.values())
137-
.forEach(command -> assertThat(command.requiresExactNumberOfArguments())
138-
.describedAs("Redis command [%s] failed requires exact arguments check".formatted(command.name())).isEqualTo(
139-
ReflectionTestUtils.getField(command, "minArgs") == ReflectionTestUtils.getField(command, "maxArgs")));
135+
assertThat(command.requiresExactNumberOfArguments())
136+
.describedAs("Redis command [%s] failed requires exact arguments check".formatted(command.name())).isEqualTo(
137+
ReflectionTestUtils.getField(command, "minArgs") == ReflectionTestUtils.getField(command, "maxArgs"));
140138
}
141139

142140
}

src/test/java/org/springframework/data/redis/core/TransactionalStringRedisTemplateTests.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package org.springframework.data.redis.core;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.Mockito.*;
1920

2021
import java.sql.Connection;
2122
import java.sql.SQLException;
@@ -35,6 +36,7 @@
3536
import org.junit.jupiter.params.provider.Arguments;
3637
import org.junit.jupiter.params.provider.MethodSource;
3738
import org.mockito.Mockito;
39+
3840
import org.springframework.context.Lifecycle;
3941
import org.springframework.data.redis.SettingsUtils;
4042
import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -44,14 +46,16 @@
4446
import org.springframework.transaction.support.TransactionTemplate;
4547

4648
/**
49+
* Transactional integration tests for {@link StringRedisTemplate}.
50+
*
4751
* @author Christoph Strobl
4852
*/
4953
@ParameterizedClass
5054
@MethodSource("argumentsStream")
51-
public class TransactionalStringRedisTemplateTests {
55+
class TransactionalStringRedisTemplateTests {
5256

53-
RedisConnectionFactory redisConnectionFactory;
54-
StringRedisTemplate stringTemplate;
57+
private RedisConnectionFactory redisConnectionFactory;
58+
private StringRedisTemplate stringTemplate;
5559

5660
TransactionalStringRedisTemplateTests(RedisConnectionFactory redisConnectionFactory) {
5761
this.redisConnectionFactory = redisConnectionFactory;
@@ -86,8 +90,8 @@ void visibilityDuringManagedTransaction() throws SQLException {
8690

8791
stringTemplate.opsForSet().add("myset", "outside");
8892

89-
DataSource ds = Mockito.mock(DataSource.class);
90-
Mockito.when(ds.getConnection()).thenReturn(Mockito.mock(Connection.class));
93+
DataSource ds = mock(DataSource.class);
94+
Mockito.when(ds.getConnection()).thenReturn(mock(Connection.class));
9195

9296
DataSourceTransactionManager txMgr = new DataSourceTransactionManager(ds);
9397

0 commit comments

Comments
 (0)