Skip to content

Commit e4e2f03

Browse files
committed
fixing tests
1 parent 58fd4ec commit e4e2f03

File tree

3 files changed

+101
-90
lines changed

3 files changed

+101
-90
lines changed

build.gradle.kts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ dependencies {
3333
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactive")
3434
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
3535

36-
testImplementation("org.springframework.boot:spring-boot-starter-test") {
37-
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
38-
}
36+
testImplementation("org.springframework.boot:spring-boot-starter-test")
3937

4038
runtimeOnly("com.h2database:h2")
4139

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.example.kotlin.chat
22

33
import io.r2dbc.spi.ConnectionFactory
4+
import org.springframework.beans.factory.annotation.Qualifier
45
import org.springframework.boot.autoconfigure.SpringBootApplication
56
import org.springframework.boot.runApplication
67
import org.springframework.context.annotation.Bean
8+
import org.springframework.context.annotation.Configuration
79
import org.springframework.core.io.ClassPathResource
810
import org.springframework.r2dbc.connection.init.CompositeDatabasePopulator
911
import org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer
@@ -16,12 +18,15 @@ fun main(args: Array<String>) {
1618
runApplication<ChatKotlinApplication>(*args)
1719
}
1820

19-
@Bean
20-
fun initializer(connectionFactory: ConnectionFactory): ConnectionFactoryInitializer {
21-
val initializer = ConnectionFactoryInitializer()
22-
initializer.setConnectionFactory(connectionFactory)
23-
val populator = CompositeDatabasePopulator()
24-
populator.addPopulators(ResourceDatabasePopulator(ClassPathResource("./sql/schema.sql")))
25-
initializer.setDatabasePopulator(populator)
26-
return initializer
21+
@Configuration
22+
class Config {
23+
@Bean
24+
fun initializer(connectionFactory: ConnectionFactory): ConnectionFactoryInitializer {
25+
val initializer = ConnectionFactoryInitializer()
26+
initializer.setConnectionFactory(connectionFactory)
27+
val populator = CompositeDatabasePopulator()
28+
populator.addPopulators(ResourceDatabasePopulator(ClassPathResource("./sql/schema.sql")))
29+
initializer.setDatabasePopulator(populator)
30+
return initializer
31+
}
2732
}

src/test/kotlin/com/example/kotlin/chat/ChatKotlinApplicationTests.kt

Lines changed: 87 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -44,102 +44,110 @@ class ChatKotlinApplicationTests {
4444
val now: Instant = Instant.now()
4545

4646
@BeforeEach
47-
fun setUp() = runBlocking {
48-
val secondBeforeNow = now.minusSeconds(1)
49-
val twoSecondBeforeNow = now.minusSeconds(2)
50-
val savedMessages = messageRepository.saveAll(
51-
listOf(
52-
Message(
53-
"*testMessage*",
54-
ContentType.PLAIN,
55-
twoSecondBeforeNow,
56-
"test",
57-
"http://test.com"
58-
),
59-
Message(
60-
"**testMessage2**",
61-
ContentType.MARKDOWN,
62-
secondBeforeNow,
63-
"test1",
64-
"http://test.com"
65-
),
66-
Message(
67-
"`testMessage3`",
68-
ContentType.MARKDOWN,
69-
now,
70-
"test2",
71-
"http://test.com"
47+
fun setUp() {
48+
runBlocking {
49+
val secondBeforeNow = now.minusSeconds(1)
50+
val twoSecondBeforeNow = now.minusSeconds(2)
51+
val savedMessages = messageRepository.saveAll(
52+
listOf(
53+
Message(
54+
"*testMessage*",
55+
ContentType.PLAIN,
56+
twoSecondBeforeNow,
57+
"test",
58+
"http://test.com"
59+
),
60+
Message(
61+
"**testMessage2**",
62+
ContentType.MARKDOWN,
63+
secondBeforeNow,
64+
"test1",
65+
"http://test.com"
66+
),
67+
Message(
68+
"`testMessage3`",
69+
ContentType.MARKDOWN,
70+
now,
71+
"test2",
72+
"http://test.com"
73+
)
7274
)
7375
)
74-
)
75-
lastMessageId = savedMessages.first().id ?: ""
76+
lastMessageId = savedMessages.first().id ?: ""
77+
}
7678
}
7779

7880
@AfterEach
79-
fun tearDown() = runBlocking {
80-
messageRepository.deleteAll()
81+
fun tearDown() {
82+
runBlocking {
83+
messageRepository.deleteAll()
84+
}
8185
}
8286

8387
@ParameterizedTest
8488
@ValueSource(booleans = [true, false])
85-
fun `test that messages API returns latest messages`(withLastMessageId: Boolean) = runBlocking{
86-
val messages: List<MessageVM>? = client.exchange(
87-
RequestEntity<Any>(
88-
HttpMethod.GET,
89-
URI("/api/v1/messages?lastMessageId=${if (withLastMessageId) lastMessageId else ""}")
90-
),
91-
object : ParameterizedTypeReference<List<MessageVM>>() {}).body
89+
fun `test that messages API returns latest messages`(withLastMessageId: Boolean) {
90+
runBlocking {
91+
92+
val messages: List<MessageVM>? = client.exchange(
93+
RequestEntity<Any>(
94+
HttpMethod.GET,
95+
URI("/api/v1/messages?lastMessageId=${if (withLastMessageId) lastMessageId else ""}")
96+
),
97+
object : ParameterizedTypeReference<List<MessageVM>>() {}).body
98+
99+
if (!withLastMessageId) {
100+
assertThat(messages?.map { with(it) { copy(id = null, sent = sent.truncatedTo(MILLIS)) } })
101+
.first()
102+
.isEqualTo(
103+
MessageVM(
104+
"*testMessage*",
105+
UserVM("test", URL("http://test.com")),
106+
now.minusSeconds(2).truncatedTo(MILLIS)
107+
)
108+
)
109+
}
92110

93-
if (!withLastMessageId) {
94-
assertThat(messages?.map { with(it) { copy(id = null, sent = sent.truncatedTo(MILLIS)) } })
95-
.first()
96-
.isEqualTo(
111+
assertThat(messages?.map { it.prepareForTesting() })
112+
.containsSubsequence(
97113
MessageVM(
98-
"*testMessage*",
99-
UserVM("test", URL("http://test.com")),
100-
now.minusSeconds(2).truncatedTo(MILLIS)
114+
"<body><p><strong>testMessage2</strong></p></body>",
115+
UserVM("test1", URL("http://test.com")),
116+
now.minusSeconds(1).truncatedTo(MILLIS)
117+
),
118+
MessageVM(
119+
"<body><p><code>testMessage3</code></p></body>",
120+
UserVM("test2", URL("http://test.com")),
121+
now.truncatedTo(MILLIS)
101122
)
102123
)
103124
}
104-
105-
assertThat(messages?.map { it.prepareForTesting() })
106-
.containsSubsequence(
107-
MessageVM(
108-
"<body><p><strong>testMessage2</strong></p></body>",
109-
UserVM("test1", URL("http://test.com")),
110-
now.minusSeconds(1).truncatedTo(MILLIS)
111-
),
112-
MessageVM(
113-
"<body><p><code>testMessage3</code></p></body>",
114-
UserVM("test2", URL("http://test.com")),
115-
now.truncatedTo(MILLIS)
116-
)
117-
)
118125
}
119126

120127

121128
@Test
122-
fun `test that messages posted to the API is stored`() = runBlocking {
123-
client.postForEntity<Any>(
124-
URI("/api/v1/messages"),
125-
MessageVM(
126-
"`HelloWorld`",
127-
UserVM("test", URL("http://test.com")),
128-
now.plusSeconds(1)
129-
)
130-
)
131-
132-
messageRepository.findAll()
133-
.first { it.content.contains("HelloWorld") }
134-
.apply {
135-
assertThat(this.prepareForTesting())
136-
.isEqualTo(Message(
137-
"`HelloWorld`",
138-
ContentType.MARKDOWN,
139-
now.plusSeconds(1).truncatedTo(MILLIS),
140-
"test",
141-
"http://test.com"
142-
))
143-
}
129+
fun `test that messages posted to the API is stored`() {
130+
runBlocking {
131+
client.postForEntity<Any>(
132+
URI("/api/v1/messages"),
133+
MessageVM(
134+
"`HelloWorld`",
135+
UserVM("test", URL("http://test.com")),
136+
now.plusSeconds(1)
137+
)
138+
)
139+
messageRepository.findAll()
140+
.first { it.content.contains("HelloWorld") }
141+
.apply {
142+
assertThat(this.prepareForTesting())
143+
.isEqualTo(Message(
144+
"`HelloWorld`",
145+
ContentType.MARKDOWN,
146+
now.plusSeconds(1).truncatedTo(MILLIS),
147+
"test",
148+
"http://test.com"
149+
))
150+
}
151+
}
144152
}
145153
}

0 commit comments

Comments
 (0)