1
+ package demo
2
+
3
+ import demo.kx.uuid
4
+ 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
9
+ import org.springframework.beans.factory.annotation.Autowired
10
+ import org.springframework.boot.test.context.SpringBootTest
11
+ import org.springframework.boot.test.web.client.TestRestTemplate
12
+ import org.springframework.boot.test.web.client.getForEntity
13
+ import org.springframework.boot.test.web.client.getForObject
14
+ import org.springframework.boot.test.web.client.postForObject
15
+ import org.springframework.http.HttpStatus
16
+ import org.springframework.test.context.DynamicPropertyRegistry
17
+ import org.springframework.test.context.DynamicPropertySource
18
+ import org.testcontainers.junit.jupiter.Container
19
+ import org.testcontainers.junit.jupiter.Testcontainers
20
+ import kotlin.random.Random
21
+
22
+ @Testcontainers
23
+ @SpringBootTest(
24
+ webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT ,
25
+ )
26
+ @TestMethodOrder(MethodOrderer .OrderAnnotation ::class )
27
+ class SpringBootIntegrationTests (@Autowired val client : TestRestTemplate ) {
28
+ companion object {
29
+ @Container
30
+ val container = postgres(" postgres:13-alpine" ) {
31
+ withInitScript(" sql/schema.ddl" )
32
+ withDatabaseName(" db" )
33
+ withUsername(" user" )
34
+ withPassword(" password" )
35
+ }
36
+
37
+ @JvmStatic
38
+ @DynamicPropertySource
39
+ fun datasourceConfig (registry : DynamicPropertyRegistry ) {
40
+ registry.add(" spring.datasource.url" , container::getJdbcUrl)
41
+ registry.add(" spring.datasource.password" , container::getPassword)
42
+ registry.add(" spring.datasource.username" , container::getUsername)
43
+ }
44
+ }
45
+
46
+ @Test
47
+ @Order(1 )
48
+ fun `test hello endpoint` () {
49
+ println (" >>>>>>>>>>>>> Asserting Hello endpoint! >>>>>>>>>>>>>>>>>>>>>>>>> " )
50
+ val entity = client.getForEntity<String >(" /hello" )
51
+ Assertions .assertThat(entity.statusCode).isEqualTo(HttpStatus .OK )
52
+ Assertions .assertThat(entity.body).contains(" Hello" )
53
+ }
54
+
55
+ // TODO: remove
56
+ val id = " e3b7f959-f5c2-3d07-86e3-cd9df75e0b83" // "${Random.nextInt()}".uuid()
57
+
58
+ @Test
59
+ @Order(2 )
60
+ fun `testing if we can post and retrieve the data` () {
61
+ // val id = "${Random.nextInt()}".uuid()
62
+ val message = Message (id, " some message" )
63
+ client.postForObject<Message >(" /" , message)
64
+
65
+ val entity = client.getForEntity<String >(" /$id " )
66
+ Assertions .assertThat(entity.statusCode).isEqualTo(HttpStatus .OK )
67
+ Assertions .assertThat(entity.body).contains(message.id)
68
+ Assertions .assertThat(entity.body).contains(message.text)
69
+
70
+ val msg = client.getForObject<Message >(" /$id " )!!
71
+ Assertions .assertThat(entity.statusCode).isEqualTo(HttpStatus .OK )
72
+ Assertions .assertThat(msg.id).isEqualTo(message.id)
73
+ Assertions .assertThat(msg.text).contains(message.text)
74
+ }
75
+
76
+ @Test
77
+ @Order(3 )
78
+ fun `message not found` () {
79
+ // val id = "${Random.nextInt()}".uuid()
80
+ val entity = client.getForEntity<String >(" /$id " )
81
+ Assertions .assertThat(entity.statusCode).isEqualTo(HttpStatus .NOT_FOUND )
82
+ }
83
+ }
0 commit comments