-
Notifications
You must be signed in to change notification settings - Fork 0
/
FlywayMigrationAuthorRepositoryTest.java
101 lines (85 loc) · 3.68 KB
/
FlywayMigrationAuthorRepositoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.example.testing;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.test.annotation.Commit;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration test that uses {@link CleanDatabaseTestExecutionListener} to reinitialise the database after each test.
*/
@JdbcTest
@ContextConfiguration(
initializers = FlywayMigrationAuthorRepositoryTest.class,
// Whether explicit inclusion of @Repository and @TestConfiguration is necessary depends on the test slice
// (like @JdbcTest) being used. Some slices automatically scan for those. Check the Spring Boot documentation on the
// test slice you are using.
classes = {
AuthorRepository.class,
FlywayMigrationAuthorRepositoryTest.AdditionalTestConfiguration.class
}
)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
// An alternative to a TestExecutionListener would be a setup method annotated with @BeforeEach that invokes Flyway. But
// then you would lose the ability to access the database in any TestExecutionListener (incl. the one that processes
// @Sql annotations) because @BeforeEach runs after the TestExecutionListeners.
@TestExecutionListeners(
value = {CleanDatabaseTestExecutionListener.class},
mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS // Retains default TestExecutionListeners.
)
@Commit
class FlywayMigrationAuthorRepositoryTest extends AbstractPostgresJupiterTest {
@Autowired
AuthorRepository authorRepository;
@Test
void findAll_returnsAllAuthors() {
var authors = this.authorRepository.findAll();
assertThat(authors)
.extracting(Author::name)
.containsExactly("Bert Bates", "Joshua Bloch", "Kathy Sierra", "Trisha Gee");
}
@Test
void add_insertsAuthor() {
var author = new Author(AuthorId.from("82f4870d-f2e5-4a9f-a2b2-b297f66733a0"), "Brian Goetz");
this.authorRepository.add(author);
assertThat(this.authorRepository.findAll())
.extracting(Author::name)
.containsExactly("Bert Bates", "Brian Goetz", "Joshua Bloch", "Kathy Sierra", "Trisha Gee");
}
@Test
void delete_ignoresNullAuthorId() {
this.authorRepository.delete(null);
assertThat(this.authorRepository.findAll())
.extracting(Author::name)
.containsExactly("Bert Bates", "Joshua Bloch", "Kathy Sierra", "Trisha Gee");
}
@Test
void delete_removesAuthor() {
this.authorRepository.delete(AuthorId.from("1a0f9c80-1309-4e9d-a291-752354b51c51"));
assertThat(this.authorRepository.findAll())
.extracting(Author::name)
.containsExactly("Bert Bates", "Kathy Sierra", "Trisha Gee");
}
@Test
void delete_hasNoEffectWhenAuthorIsUnknown() {
this.authorRepository.delete(AuthorId.from("06f4cec2-d26d-43ac-9cd5-351825bf2af9"));
assertThat(this.authorRepository.findAll())
.extracting(Author::name)
.containsExactly("Bert Bates", "Joshua Bloch", "Kathy Sierra", "Trisha Gee");
}
@TestConfiguration
public static class AdditionalTestConfiguration {
@Bean
public FlywayMigrationStrategy flywayMigrationStrategy() {
return flyway -> {
// Do nothing to disable the Flyway migration action on startup without having to disable the Flyway
// autoconfiguration which is what spring.flyway.enabled=false would do.
};
}
}
}