Skip to content

Commit ffa8a69

Browse files
committed
- solution for the ordered tests issue
- disabled DemoApplicationTests
1 parent 95af6b7 commit ffa8a69

File tree

7 files changed

+39
-20
lines changed

7 files changed

+39
-20
lines changed

README.md

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
# Spring Time in Kotlin, Episode 4: Testing with JUnit
1+
# Spring Time in Kotlin, Episode 5: Integration testing with TestContainers
22

3-
This is a demo project used in [the 4th episode of Spring Time in Kotlin series](https://www.youtube.com/watch?v=0jWo3o7r-W4) at the official [Kotlin YouTube channel](https://www.youtube.com/kotlin)
3+
This is a demo project used in [the 5th episode of Spring Time in Kotlin series](https://www.youtube.com/watch?v=0jWo3o7r-W4) at the official [Kotlin YouTube channel](https://www.youtube.com/kotlin)
44

5-
Lean how to use JUnit with Kotlin. Implement a few integration tests for a Spring app and add some web layer tests with MockMVC with MockK and SpringMockK.
5+
In this episode:
6+
- We start using PostgreSQL database instead of H2
7+
- Use TestContainers for testing
8+
- Lean more about Kotlin and Java interoperability
9+
10+
##Running the application
11+
12+
First, start PostgreSQL database by running this command (assuming you have Docker installed on the machine):
13+
14+
docker run -e POSTGRES_PASSWORD=password -e POSTGRES_USER=sa -p 5432:5432 -d postgres
15+
16+
Now you can launch Spring Boot application:
17+
18+
./gradlew bootRun

requests.http

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ POST http://localhost:8080
1919
Content-Type: application/json
2020

2121
{
22-
"text": "Privet!"
22+
"text": "Privet2!"
2323
}
2424

2525
### get all the messages
+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#spring.datasource.driver-class-name=org.h2.Driver
21
spring.datasource.driver-class-name=org.postgresql.Driver
3-
spring.datasource.url=jdbc:h2:file:./data/testdb
2+
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
43
spring.datasource.username=sa
54
spring.datasource.password=password
6-
spring.datasource.schema=classpath:sql/schema.sql
5+
spring.datasource.schema=classpath:sql/schema.ddl
76
spring.datasource.initialization-mode=always

src/main/resources/sql/schema.ddl

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
CREATE TABLE messages (
2-
id VARCHAR(100) UNIQUE NOT NULL,
1+
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
2+
3+
CREATE TABLE IF NOT EXISTS messages (
4+
id varchar(50) DEFAULT uuid_generate_v4()::text,
35
text VARCHAR(500),
46
CONSTRAINT id_messages PRIMARY KEY (id)
57
);

src/test/kotlin/demo/DemoApplicationTests.kt

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package demo
22

33
import demo.kx.uuid
44
import org.assertj.core.api.Assertions.assertThat
5+
import org.junit.jupiter.api.Disabled
56
import org.junit.jupiter.api.Test
67
import org.springframework.beans.factory.annotation.Autowired
78
import org.springframework.boot.test.context.SpringBootTest
@@ -18,6 +19,7 @@ import kotlin.random.Random
1819
"spring.datasource.url=jdbc:h2:mem:testdb"
1920
]
2021
)
22+
@Disabled
2123
class DemoApplicationTests(@Autowired val client: TestRestTemplate) {
2224

2325
@Test

src/test/kotlin/demo/IntegrationTests.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class IntegrationTests {
1111
@Container
1212
var container = postgres("postgres:13-alpine") {
1313
// withExposedPorts(6379)
14-
withInitScript("schema.ddl")
14+
withInitScript("sql/schema.ddl")
1515
withDatabaseName("db")
1616
withUsername("user")
1717
withPassword("password")

src/test/kotlin/demo/SpringBootIntegrationTests.kt

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
package demo
22

3-
import demo.kx.uuid
43
import org.assertj.core.api.Assertions
5-
import org.junit.jupiter.api.MethodOrderer
6-
import org.junit.jupiter.api.Order
7-
import org.junit.jupiter.api.Test
8-
import org.junit.jupiter.api.TestMethodOrder
4+
import org.junit.jupiter.api.*
95
import org.springframework.beans.factory.annotation.Autowired
106
import org.springframework.boot.test.context.SpringBootTest
117
import org.springframework.boot.test.web.client.TestRestTemplate
128
import org.springframework.boot.test.web.client.getForEntity
139
import org.springframework.boot.test.web.client.getForObject
1410
import org.springframework.boot.test.web.client.postForObject
1511
import org.springframework.http.HttpStatus
12+
import org.springframework.jdbc.core.JdbcTemplate
1613
import org.springframework.test.context.DynamicPropertyRegistry
1714
import org.springframework.test.context.DynamicPropertySource
1815
import org.testcontainers.junit.jupiter.Container
1916
import org.testcontainers.junit.jupiter.Testcontainers
20-
import kotlin.random.Random
2117

2218
@Testcontainers
2319
@SpringBootTest(
2420
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
2521
)
2622
@TestMethodOrder(MethodOrderer.OrderAnnotation::class)
27-
class SpringBootIntegrationTests(@Autowired val client: TestRestTemplate) {
23+
class SpringBootIntegrationTests(
24+
@Autowired val client: TestRestTemplate,
25+
@Autowired val jdbc: JdbcTemplate,
26+
) {
2827
companion object {
2928
@Container
3029
val container = postgres("postgres:13-alpine") {
@@ -43,17 +42,21 @@ class SpringBootIntegrationTests(@Autowired val client: TestRestTemplate) {
4342
}
4443
}
4544

45+
@AfterEach
46+
fun cleanup() {
47+
jdbc.execute("truncate table messages")
48+
}
49+
4650
@Test
4751
@Order(1)
4852
fun `test hello endpoint`() {
49-
println(">>>>>>>>>>>>> Asserting Hello endpoint! >>>>>>>>>>>>>>>>>>>>>>>>> ")
5053
val entity = client.getForEntity<String>("/hello")
5154
Assertions.assertThat(entity.statusCode).isEqualTo(HttpStatus.OK)
5255
Assertions.assertThat(entity.body).contains("Hello")
5356
}
5457

55-
//TODO: remove
56-
val id = "e3b7f959-f5c2-3d07-86e3-cd9df75e0b83"//"${Random.nextInt()}".uuid()
58+
//TODO: this is just to demonstrate the issue with the database re-use between tests
59+
val id = "this is wrong!!!!"//"${Random.nextInt()}".uuid()
5760

5861
@Test
5962
@Order(2)

0 commit comments

Comments
 (0)