Skip to content

Commit 5ee6c20

Browse files
committed
Coverage, coverage, coverage
1 parent 4e6638d commit 5ee6c20

File tree

14 files changed

+292
-65
lines changed

14 files changed

+292
-65
lines changed

src/main/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/conf/RedisConf.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ private const val KEYWORD_BIND = "bind"
88
*/
99
internal data class RedisConf(val directives: List<Directive>) {
1010

11-
fun getPorts(): List<Int> {
12-
return getDirectives(KEYWORD_PORT).map { it.arguments.first().toInt() }
13-
}
14-
1511
fun getBinds(): List<String> {
1612
return getDirectives(KEYWORD_BIND).map { it.arguments.first() }
1713
}

src/main/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/conf/RedisConfLocator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ internal object RedisConfLocator {
1717

1818
@Suppress("UNCHECKED_CAST")
1919
internal fun locate(server: Redis): Path {
20-
val args = ARGS_PROP[server as RedisInstance] as List<String>?
21-
return args?.find { it.endsWith(".conf") }?.let { Paths.get(it) }
20+
val args = ARGS_PROP[server as RedisInstance] as List<String>
21+
return args.find { it.endsWith(".conf") }?.let { Paths.get(it) }
2222
?: throw IllegalStateException("No config file found for embedded Redis server: $server")
2323
}
2424
}

src/main/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/junit/extension/RedisFlushAllExtension.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ internal class RedisFlushAllExtension : AfterEachCallback, AfterAllCallback {
2727
}
2828

2929
private fun flushAllMode(extensionContext: ExtensionContext?): RedisFlushAll.Mode {
30-
return extensionContext!!.requiredTestClass!!.getAnnotation(RedisFlushAll::class.java)?.mode ?: AFTER_METHOD
30+
val mode = extensionContext!!.requiredTestClass.getAnnotation(RedisFlushAll::class.java)?.mode
31+
return mode ?: AFTER_METHOD
3132
}
3233

3334
private fun flushAll(extensionContext: ExtensionContext?) {
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package io.github.tobi.laa.spring.boot.embedded.redis
2+
3+
import io.github.tobi.laa.spring.boot.embedded.redis.server.EmbeddedRedisServer
4+
import org.junit.jupiter.api.*
5+
import org.springframework.beans.factory.annotation.Autowired
6+
7+
@Nested
8+
@IntegrationTest
9+
@EmbeddedRedisServer
10+
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
11+
@TestClassOrder(ClassOrderer.OrderAnnotation::class)
12+
@DisplayName("Test @RedisFlushAll annotation")
13+
internal class FlushAllTest {
14+
15+
@Autowired
16+
private lateinit var given: RedisTests
17+
18+
@Nested
19+
@DisplayName("Not specifying @FlushAll should default to flushing all data after each test")
20+
@Order(1)
21+
inner class NotAnnotated {
22+
23+
@Test
24+
@DisplayName("It should be possible to write to Redis and the data should be available afterwards")
25+
@Order(1)
26+
fun givenRandomTestdata_writingToRedis_dataShouldBeAvailable() {
27+
given.randomTestdata()
28+
.whenRedis().isBeingWrittenTo()
29+
.then().redis().shouldContainTheTestdata()
30+
}
31+
32+
@Test
33+
@DisplayName("Redis should have been flushed after the first test")
34+
@Order(2)
35+
fun redisShouldHaveBeenFlushed() {
36+
given.nothing()
37+
.whenDoingNothing()
38+
.then().redis().shouldNotContainAnyTestdata()
39+
}
40+
}
41+
42+
@Nested
43+
@RedisFlushAll
44+
@DisplayName("Specifying @RedisFlushAll with default settings should flush all data after each test")
45+
@Order(2)
46+
inner class Default {
47+
48+
@Test
49+
@DisplayName("It should be possible to write to Redis and the data should be available afterwards")
50+
@Order(1)
51+
fun givenRandomTestdata_writingToRedis_dataShouldBeAvailable() {
52+
given.randomTestdata()
53+
.whenRedis().isBeingWrittenTo()
54+
.then().redis().shouldContainTheTestdata()
55+
}
56+
57+
@Test
58+
@DisplayName("Redis should have been flushed after the first test")
59+
@Order(2)
60+
fun redisShouldHaveBeenFlushed() {
61+
given.nothing()
62+
.whenDoingNothing()
63+
.then().redis().shouldNotContainAnyTestdata()
64+
}
65+
}
66+
67+
@Nested
68+
@RedisFlushAll(RedisFlushAll.Mode.AFTER_CLASS)
69+
@DisplayName("Specifying @RedisFlushAll with mode AFTER_CLASS should flush all data after the test class")
70+
@Order(3)
71+
inner class AfterClass {
72+
73+
@Test
74+
@DisplayName("It should be possible to write to Redis and the data should be available afterwards")
75+
@Order(1)
76+
fun givenRandomTestdata_writingToRedis_dataShouldBeAvailable() {
77+
given.randomTestdata()
78+
.whenRedis().isBeingWrittenTo()
79+
.then().redis().shouldContainTheTestdata()
80+
}
81+
82+
@Test
83+
@DisplayName("Redis should not have been flushed after the first test")
84+
@Order(2)
85+
fun redisShouldNotHaveBeenFlushed() {
86+
given.nothing()
87+
.whenDoingNothing()
88+
.then().redis().shouldContainTheTestdata()
89+
}
90+
}
91+
92+
@Nested
93+
@DisplayName("The assertion for the previously executed AfterClass")
94+
@Order(4)
95+
inner class AfterClassAssertion {
96+
97+
@Test
98+
@DisplayName("Redis should have been flushed after the test class 'AfterClass'")
99+
fun redisShouldHaveBeenFlushed() {
100+
given.nothing()
101+
.whenDoingNothing()
102+
.then().redis().shouldNotContainAnyTestdata()
103+
}
104+
}
105+
106+
@Nested
107+
@RedisFlushAll(RedisFlushAll.Mode.NEVER)
108+
@DisplayName("Specifying @RedisFlushAll with mode NEVER should not flush any data")
109+
@Order(5)
110+
inner class Never {
111+
112+
@Test
113+
@DisplayName("It should be possible to write to Redis and the data should be available afterwards")
114+
@Order(1)
115+
fun givenRandomTestdata_writingToRedis_dataShouldBeAvailable() {
116+
given.randomTestdata()
117+
.whenRedis().isBeingWrittenTo()
118+
.then().redis().shouldContainTheTestdata()
119+
}
120+
121+
@Test
122+
@DisplayName("Redis should not have been flushed after the first test")
123+
@Order(2)
124+
fun redisShouldNotHaveBeenFlushed() {
125+
given.nothing()
126+
.whenDoingNothing()
127+
.then().redis().shouldContainTheTestdata()
128+
}
129+
}
130+
131+
@Nested
132+
@DisplayName("The assertion for the previously executed Never")
133+
@Order(6)
134+
inner class NeverAssertion {
135+
136+
@Test
137+
@DisplayName("Redis should not have been flushed after the test class 'Never'")
138+
fun redisShouldNotHaveBeenFlushed() {
139+
given.nothing()
140+
.whenDoingNothing()
141+
.then().redis().shouldContainTheTestdata()
142+
}
143+
}
144+
}
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.github.tobi.laa.spring.boot.embedded.redis
22

3-
import org.junit.jupiter.api.MethodOrderer
4-
import org.junit.jupiter.api.TestMethodOrder
53
import org.springframework.boot.test.context.SpringBootTest
64

75
/**
@@ -11,5 +9,4 @@ import org.springframework.boot.test.context.SpringBootTest
119
@Retention(AnnotationRetention.RUNTIME)
1210
@MustBeDocumented
1311
@SpringBootTest
14-
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
1512
annotation class IntegrationTest
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.github.tobi.laa.spring.boot.embedded.redis.conf
2+
3+
import org.assertj.core.api.Assertions.assertThatThrownBy
4+
import org.junit.jupiter.api.DisplayName
5+
import org.junit.jupiter.api.Test
6+
import redis.embedded.RedisServer
7+
8+
@DisplayName("Tests for RedisConfLocator")
9+
internal class RedisConfLocatorTest {
10+
11+
@Test
12+
@DisplayName("RedisConfLocator should throw exception for missing file")
13+
fun missingFile_shouldThrowException() {
14+
val redis = RedisServer(6379, emptyList(), false)
15+
assertThatThrownBy { RedisConfLocator.locate(redis) }
16+
.isExactlyInstanceOf(IllegalStateException::class.java)
17+
.hasMessage("No config file found for embedded Redis server: $redis")
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.tobi.laa.spring.boot.embedded.redis.conf
2+
3+
import org.assertj.core.api.Assertions.assertThatThrownBy
4+
import org.junit.jupiter.api.DisplayName
5+
import org.junit.jupiter.api.Test
6+
7+
@DisplayName("Tests for RedisConf")
8+
internal class RedisConfTest {
9+
10+
@Test
11+
@DisplayName("RedisConf should throw exception for blank keyword")
12+
fun blankKeyword_shouldThrowException() {
13+
assertThatThrownBy { RedisConf(listOf(RedisConf.Directive(""))) }
14+
.isInstanceOf(IllegalArgumentException::class.java)
15+
.hasMessage("Keyword must not be blank")
16+
}
17+
18+
@Test
19+
@DisplayName("RedisConf should throw exception for missing argument")
20+
fun missingArgument_shouldThrowException() {
21+
assertThatThrownBy { RedisConf(listOf(RedisConf.Directive("dummy"))) }
22+
.isInstanceOf(IllegalArgumentException::class.java)
23+
.hasMessage("At least one argument is required")
24+
}
25+
}

src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/server/CustomizerTest.kt renamed to src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/server/CustomCustomizerTest.kt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ package io.github.tobi.laa.spring.boot.embedded.redis.server
22

33
import io.github.tobi.laa.spring.boot.embedded.redis.IntegrationTest
44
import io.github.tobi.laa.spring.boot.embedded.redis.RedisTests
5-
import io.github.tobi.laa.spring.boot.embedded.redis.server.CustomizerTest.*
5+
import io.github.tobi.laa.spring.boot.embedded.redis.server.CustomCustomizerTest.*
66
import org.junit.jupiter.api.DisplayName
7-
import org.junit.jupiter.api.Order
87
import org.junit.jupiter.api.Test
98
import org.springframework.beans.factory.annotation.Autowired
109
import redis.embedded.core.RedisServerBuilder
@@ -20,14 +19,13 @@ private const val TEST_PORT = 10000
2019
SetsProtectedMode::class]
2120
)
2221
@DisplayName("Using @EmbeddedRedisServer with several customizers")
23-
internal open class CustomizerTest {
22+
internal open class CustomCustomizerTest {
2423

2524
@Autowired
2625
private lateinit var given: RedisTests
2726

2827
@Test
2928
@DisplayName("RedisProperties should have the customized values")
30-
@Order(1)
3129
fun redisPropertiesShouldHaveCustomizedValues() {
3230
given.nothing()
3331
.whenDoingNothing()
@@ -39,25 +37,14 @@ internal open class CustomizerTest {
3937

4038
@Test
4139
@DisplayName("It should be possible to write to Redis and the data should be available afterwards")
42-
@Order(2)
4340
fun givenRandomTestdata_writingToRedis_dataShouldBeAvailable() {
4441
given.randomTestdata()
4542
.whenRedis().isBeingWrittenTo()
4643
.then().redis().shouldContainTheTestdata()
4744
}
4845

49-
@Test
50-
@DisplayName("Redis should have been flushed after the first test")
51-
@Order(3)
52-
fun redisShouldHaveBeenFlushed() {
53-
given.nothing()
54-
.whenDoingNothing()
55-
.then().redis().shouldNotContainAnyTestdata()
56-
}
57-
5846
@Test
5947
@DisplayName("Settings from customizer should have been applied to the embedded Redis server")
60-
@Order(4)
6148
fun settingsFromCustomizerShouldHaveBeenApplied() {
6249
given.nothing()
6350
.whenDoingNothing()

src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/server/CustomSettingsTest.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package io.github.tobi.laa.spring.boot.embedded.redis.server
33
import io.github.tobi.laa.spring.boot.embedded.redis.IntegrationTest
44
import io.github.tobi.laa.spring.boot.embedded.redis.RedisTests
55
import org.junit.jupiter.api.DisplayName
6-
import org.junit.jupiter.api.Order
76
import org.junit.jupiter.api.Test
87
import org.springframework.beans.factory.annotation.Autowired
98

@@ -27,7 +26,6 @@ internal class CustomSettingsTest {
2726

2827
@Test
2928
@DisplayName("RedisProperties should have the customized values")
30-
@Order(1)
3129
fun redisPropertiesShouldHaveCustomizedValues() {
3230
given.nothing()
3331
.whenDoingNothing()
@@ -39,25 +37,14 @@ internal class CustomSettingsTest {
3937

4038
@Test
4139
@DisplayName("It should be possible to write to Redis and the data should be available afterwards")
42-
@Order(2)
4340
fun givenRandomTestdata_writingToRedis_dataShouldBeAvailable() {
4441
given.randomTestdata()
4542
.whenRedis().isBeingWrittenTo()
4643
.then().redis().shouldContainTheTestdata()
4744
}
4845

49-
@Test
50-
@DisplayName("Redis should have been flushed after the first test")
51-
@Order(3)
52-
fun redisShouldHaveBeenFlushed() {
53-
given.nothing()
54-
.whenDoingNothing()
55-
.then().redis().shouldNotContainAnyTestdata()
56-
}
57-
5846
@Test
5947
@DisplayName("Settings from @EmbeddedRedisServer should have been applied to the embedded Redis server")
60-
@Order(4)
6148
fun configFileShouldHaveBeenApplied() {
6249
given.nothing()
6350
.whenDoingNothing()

src/test/kotlin/io/github/tobi/laa/spring/boot/embedded/redis/server/DefaultSettingsTest.kt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package io.github.tobi.laa.spring.boot.embedded.redis.server
33
import io.github.tobi.laa.spring.boot.embedded.redis.IntegrationTest
44
import io.github.tobi.laa.spring.boot.embedded.redis.RedisTests
55
import org.junit.jupiter.api.DisplayName
6-
import org.junit.jupiter.api.Order
76
import org.junit.jupiter.api.Test
87
import org.springframework.beans.factory.annotation.Autowired
98
import redis.embedded.Redis
@@ -18,7 +17,6 @@ internal open class DefaultSettingsTest {
1817

1918
@Test
2019
@DisplayName("RedisProperties should have the default values")
21-
@Order(1)
2220
fun redisPropertiesShouldHaveDefaultValues() {
2321
given.nothing()
2422
.whenDoingNothing()
@@ -30,19 +28,9 @@ internal open class DefaultSettingsTest {
3028

3129
@Test
3230
@DisplayName("It should be possible to write to Redis and the data should be available afterwards")
33-
@Order(2)
3431
fun givenRandomTestdata_writingToRedis_dataShouldBeAvailable() {
3532
given.randomTestdata()
3633
.whenRedis().isBeingWrittenTo()
3734
.then().redis().shouldContainTheTestdata()
3835
}
39-
40-
@Test
41-
@DisplayName("Redis should have been flushed after the first test")
42-
@Order(3)
43-
fun redisShouldHaveBeenFlushed() {
44-
given.nothing()
45-
.whenDoingNothing()
46-
.then().redis().shouldNotContainAnyTestdata()
47-
}
4836
}

0 commit comments

Comments
 (0)