Skip to content

Commit d56c440

Browse files
feat: use MySQL test container for integration tests (#90)
1 parent 9bade3e commit d56c440

File tree

7 files changed

+154
-77
lines changed

7 files changed

+154
-77
lines changed

pom.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3737
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
3838
<java.version>17</java.version>
39+
<testcontainers.version>1.19.1</testcontainers.version>
3940
<!-- Sonar Cloud -->
4041
<sonar.organization>igor-baiborodine-github</sonar.organization>
4142
<sonar.projectKey>igor-baiborodine_campsite-booking</sonar.projectKey>
@@ -155,6 +156,18 @@
155156
<artifactId>easy-random-core</artifactId>
156157
<version>5.0.0</version>
157158
</dependency>
159+
<dependency>
160+
<groupId>org.testcontainers</groupId>
161+
<artifactId>testcontainers</artifactId>
162+
<version>${testcontainers.version}</version>
163+
<scope>test</scope>
164+
</dependency>
165+
<dependency>
166+
<groupId>org.testcontainers</groupId>
167+
<artifactId>mysql</artifactId>
168+
<version>${testcontainers.version}</version>
169+
<scope>test</scope>
170+
</dependency>
158171
</dependencies>
159172

160173
<build>
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.kiroule.campsite.booking.api.model;
22

3-
import java.time.LocalDateTime;
3+
import java.time.Instant;
44
import lombok.Data;
55
import lombok.Generated;
66
import lombok.experimental.SuperBuilder;
@@ -10,7 +10,7 @@
1010
@Generated
1111
public abstract class DateAudit {
1212

13-
private LocalDateTime createdAt;
13+
private Instant createdAt;
1414

15-
private LocalDateTime updatedAt;
15+
private Instant updatedAt;
1616
}

src/main/java/com/kiroule/campsite/booking/api/repository/entity/DateAuditEntity.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import jakarta.persistence.Column;
44
import jakarta.persistence.EntityListeners;
55
import jakarta.persistence.MappedSuperclass;
6-
import java.time.LocalDateTime;
7-
6+
import java.time.Instant;
87
import lombok.AllArgsConstructor;
98
import lombok.Data;
109
import lombok.Generated;
@@ -25,9 +24,9 @@ public abstract class DateAuditEntity {
2524

2625
@CreatedDate
2726
@Column(name = "created_at", nullable = false, updatable = false)
28-
private LocalDateTime createdAt;
27+
private Instant createdAt;
2928

3029
@LastModifiedDate
3130
@Column(name = "updated_at")
32-
private LocalDateTime updatedAt;
31+
private Instant updatedAt;
3332
}

src/test/java/com/kiroule/campsite/booking/api/BaseIT.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,30 @@
44

55
import org.springframework.boot.test.context.SpringBootTest;
66
import org.springframework.test.context.ActiveProfiles;
7+
import org.springframework.test.context.DynamicPropertyRegistry;
8+
import org.springframework.test.context.DynamicPropertySource;
9+
import org.testcontainers.containers.MySQLContainer;
710

811
@SpringBootTest(webEnvironment = RANDOM_PORT)
9-
@ActiveProfiles("in-memory-db")
10-
public abstract class BaseIT {}
12+
@ActiveProfiles("mysql")
13+
public abstract class BaseIT {
14+
15+
private static final String MYSQL_DOCKER_IMAGE_NAME = "mysql:8-debian";
16+
private static final String MYSQL_DATABASE_NAME = "test_campsite";
17+
18+
static final MySQLContainer<?> mySqlContainer;
19+
20+
static {
21+
mySqlContainer =
22+
new MySQLContainer<>(MYSQL_DOCKER_IMAGE_NAME).withDatabaseName(MYSQL_DATABASE_NAME);
23+
mySqlContainer.start();
24+
}
25+
26+
@DynamicPropertySource
27+
static void mysqlProperties(DynamicPropertyRegistry registry) {
28+
registry.add("spring.datasource.url", mySqlContainer::getJdbcUrl);
29+
registry.add("spring.datasource.username", mySqlContainer::getUsername);
30+
registry.add("spring.datasource.password", mySqlContainer::getPassword);
31+
registry.add("spring.jpa.properties.hibernate.show_sql", () -> "true");
32+
}
33+
}

src/test/java/com/kiroule/campsite/booking/api/repository/BookingRepositoryIT.java

Lines changed: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ class FindByUuid {
3737
@Test
3838
void happy_path() {
3939
// given
40-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
41-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId());
40+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
41+
BookingEntity bookingEntity = testDataGenerator.createBookingEntity(campsiteEntity.getId());
4242
// when
43-
Optional<BookingEntity> result = classUnderTest.findByUuid(booking.getUuid());
43+
Optional<BookingEntity> result = classUnderTest.findByUuid(bookingEntity.getUuid());
4444
// then
45-
assertThat(result).hasValue(booking);
45+
assertThat(result).hasValue(bookingEntity);
4646
}
4747
}
4848

@@ -53,14 +53,15 @@ class FindForDateRange {
5353
@DisplayNamePrefix("SE|-|----|-|--")
5454
void given_booking_dates_before_range_start_date__then_no_booking_found() {
5555
// given
56-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
57-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 2);
56+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
57+
BookingEntity bookingEntity =
58+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 2);
5859
// when
5960
List<BookingEntity> result =
6061
classUnderTest.findForDateRange(
61-
booking.getStartDate().plusDays(3),
62-
booking.getStartDate().plusDays(4),
63-
campsite.getId());
62+
bookingEntity.getStartDate().plusDays(3),
63+
bookingEntity.getStartDate().plusDays(4),
64+
campsiteEntity.getId());
6465
// then
6566
assertThat(result).isEmpty();
6667
}
@@ -70,14 +71,15 @@ void given_booking_dates_before_range_start_date__then_no_booking_found() {
7071
void
7172
given_booking_start_date_before_range_start_date_and_booking_end_date_equals_to_range_start_date__then_no_booking_found() {
7273
// given
73-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
74-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 2);
74+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
75+
BookingEntity bookingEntity =
76+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 2);
7577
// when
7678
List<BookingEntity> result =
7779
classUnderTest.findForDateRange(
78-
booking.getStartDate().plusDays(2),
79-
booking.getStartDate().plusDays(3),
80-
campsite.getId());
80+
bookingEntity.getStartDate().plusDays(2),
81+
bookingEntity.getStartDate().plusDays(3),
82+
campsiteEntity.getId());
8183
// then
8284
assertThat(result).isEmpty();
8385
}
@@ -87,132 +89,140 @@ void given_booking_dates_before_range_start_date__then_no_booking_found() {
8789
void
8890
given_booking_start_date_before_range_start_date_and_booking_end_date_within_range_dates__then_booking_found() {
8991
// given
90-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
91-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 3);
92+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
93+
BookingEntity bookingEntity =
94+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 3);
9295
// when
9396
List<BookingEntity> result =
9497
classUnderTest.findForDateRange(
95-
booking.getStartDate().plusDays(2),
96-
booking.getStartDate().plusDays(4),
97-
campsite.getId());
98+
bookingEntity.getStartDate().plusDays(2),
99+
bookingEntity.getStartDate().plusDays(4),
100+
campsiteEntity.getId());
98101
// then
99-
assertThat(result).hasSize(1).contains(booking);
102+
assertThat(result).hasSize(1).contains(bookingEntity);
100103
}
101104

102105
@Test
103106
@DisplayNamePrefix("--|S|E---|-|--")
104107
void
105108
given_booking_start_date_equals_to_range_start_date_and_booking_end_date_within_range_dates__then_booking_found() {
106109
// given
107-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
108-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 2);
110+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
111+
BookingEntity bookingEntity =
112+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 2);
109113
// when
110114
List<BookingEntity> result =
111115
classUnderTest.findForDateRange(
112-
booking.getStartDate().plusDays(1),
113-
booking.getStartDate().plusDays(3),
114-
campsite.getId());
116+
bookingEntity.getStartDate().plusDays(1),
117+
bookingEntity.getStartDate().plusDays(3),
118+
campsiteEntity.getId());
115119
// then
116-
assertThat(result).hasSize(1).contains(booking);
120+
assertThat(result).hasSize(1).contains(bookingEntity);
117121
}
118122

119123
@Test
120124
@DisplayNamePrefix("--|-|S--E|-|--")
121125
void given_booking_dates_within_range_dates__then_booking_found() {
122126
// given
123-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
124-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 2, 3);
127+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
128+
BookingEntity bookingEntity =
129+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 2, 3);
125130
// when
126131
List<BookingEntity> result =
127132
classUnderTest.findForDateRange(
128-
booking.getStartDate().plusDays(1),
129-
booking.getStartDate().plusDays(4),
130-
campsite.getId());
133+
bookingEntity.getStartDate().plusDays(1),
134+
bookingEntity.getStartDate().plusDays(4),
135+
campsiteEntity.getId());
131136
// then
132-
assertThat(result).hasSize(1).contains(booking);
137+
assertThat(result).hasSize(1).contains(bookingEntity);
133138
}
134139

135140
@Test
136141
@DisplayNamePrefix("--|-|---S|E|--")
137142
void
138143
given_booking_start_date_within_range_dates_and_booking_end_date_equals_to_range_end_date__then_booking_found() {
139144
// given
140-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
141-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 2, 3);
145+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
146+
BookingEntity bookingEntity =
147+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 2, 3);
142148
// when
143149
List<BookingEntity> result =
144150
classUnderTest.findForDateRange(
145-
booking.getStartDate().plusDays(1),
146-
booking.getStartDate().plusDays(3),
147-
campsite.getId());
151+
bookingEntity.getStartDate().plusDays(1),
152+
bookingEntity.getStartDate().plusDays(3),
153+
campsiteEntity.getId());
148154
// then
149-
assertThat(result).hasSize(1).contains(booking);
155+
assertThat(result).hasSize(1).contains(bookingEntity);
150156
}
151157

152158
@Test
153159
@DisplayNamePrefix("--|-|---S|-|E-")
154160
void
155161
given_booking_start_date_before_range_end_date_and_booking_end_date_after_range_end_date__then_booking_found() {
156162
// given
157-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
158-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 2, 4);
163+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
164+
BookingEntity bookingEntity =
165+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 2, 4);
159166
// when
160167
List<BookingEntity> result =
161168
classUnderTest.findForDateRange(
162-
booking.getStartDate().plusDays(1),
163-
booking.getStartDate().plusDays(3),
164-
campsite.getId());
169+
bookingEntity.getStartDate().plusDays(1),
170+
bookingEntity.getStartDate().plusDays(3),
171+
campsiteEntity.getId());
165172
// then
166-
assertThat(result).hasSize(1).contains(booking);
173+
assertThat(result).hasSize(1).contains(bookingEntity);
167174
}
168175

169176
@Test
170177
@DisplayNamePrefix("--|-|----|S|E-")
171178
void
172179
given_booking_start_date_equals_to_range_end_date_and_booking_end_date_after_range_end_date__then_booking_found() {
173180
// given
174-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
175-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 3, 4);
181+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
182+
BookingEntity bookingEntity =
183+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 3, 4);
176184
// when
177185
List<BookingEntity> result =
178186
classUnderTest.findForDateRange(
179-
booking.getStartDate().plusDays(1),
180-
booking.getStartDate().plusDays(3),
181-
campsite.getId());
187+
bookingEntity.getStartDate().plusDays(1),
188+
bookingEntity.getStartDate().plusDays(3),
189+
campsiteEntity.getId());
182190
// then
183-
assertThat(result).hasSize(1).contains(booking);
191+
assertThat(result).hasSize(1).contains(bookingEntity);
184192
}
185193

186194
@Test
187195
@DisplayNamePrefix("--|-|----|-|SE")
188196
void given_booking_dates_after_range_end_date__then_no_booking_found() {
189197
// given
190-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
191-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 3, 4);
198+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
199+
BookingEntity bookingEntity =
200+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 3, 4);
192201
// when
193202
List<BookingEntity> result =
194203
classUnderTest.findForDateRange(
195-
booking.getStartDate().plusDays(1),
196-
booking.getStartDate().plusDays(2),
197-
campsite.getId());
204+
bookingEntity.getStartDate().plusDays(1),
205+
bookingEntity.getStartDate().plusDays(2),
206+
campsiteEntity.getId());
198207
// then
199-
assertThat(result).hasSize(1).contains(booking);
208+
assertThat(result).hasSize(1).contains(bookingEntity);
200209
}
201210

202211
@Test
203212
@DisplayNamePrefix("-S|-|----|-|E-")
204213
void given_booking_dates_overlap_range_dates__then_booking_found() {
205214
// given
206-
CampsiteEntity campsite = testDataGenerator.createCampsiteEntity();
207-
BookingEntity booking = testDataGenerator.createBookingEntity(campsite.getId(), 1, 4);
215+
CampsiteEntity campsiteEntity = testDataGenerator.createCampsiteEntity();
216+
BookingEntity bookingEntity =
217+
testDataGenerator.createBookingEntity(campsiteEntity.getId(), 1, 4);
208218
// when
209219
List<BookingEntity> result =
210220
classUnderTest.findForDateRange(
211-
booking.getStartDate().plusDays(2),
212-
booking.getStartDate().plusDays(3),
213-
campsite.getId());
221+
bookingEntity.getStartDate().plusDays(2),
222+
bookingEntity.getStartDate().plusDays(3),
223+
campsiteEntity.getId());
214224
// then
215-
assertThat(result).hasSize(1).contains(booking);
225+
assertThat(result).hasSize(1).contains(bookingEntity);
216226
}
217227
}
218228
}

0 commit comments

Comments
 (0)