使用SpringBootTest的TestContainers中的DB容器出现问题
我有一个使用TestContainers的抽象类BaseIntegrationTest。问题是,当我尝试运行一个简单的DB测试(如UserRepositoryIntSpec)时,我遇到了一个异常,这意味着计数从114开始,但不是预期的从1开始。为什么索引不从1开始?为什么每次执行设置时,我的本地db user表都是清除的,因为我希望测试是在容器中运行的,容器db的使用情况,所以只有容器表会被清除。 这绝对应该是一件容易的事,我只是错过了或不明白。我将非常感谢你的帮助 对于迁移,我使用Flyway测试Spock 条件未满足:使用SpringBootTest的TestContainers中的DB容器出现问题,spring,postgresql,spring-boot,spring-boot-test,testcontainers,Spring,Postgresql,Spring Boot,Spring Boot Test,Testcontainers,我有一个使用TestContainers的抽象类BaseIntegrationTest。问题是,当我尝试运行一个简单的DB测试(如UserRepositoryIntSpec)时,我遇到了一个异常,这意味着计数从114开始,但不是预期的从1开始。为什么索引不从1开始?为什么每次执行设置时,我的本地db user表都是清除的,因为我希望测试是在容器中运行的,容器db的使用情况,所以只有容器表会被清除。 这绝对应该是一件容易的事,我只是错过了或不明白。我将非常感谢你的帮助 对于迁移,我使用Flyway
user1.getId() == 1 && user1.getRiskcustomerid() == 1 && user1.getDateCreated() != null
| | | | |
| 114 | false false
| false
BaseIntegrationTest
@ContextConfiguration
@SpringBootTest(webEnvironment = DEFINED_PORT)
@Testcontainers
@Slf4j
abstract class BaseIntegrationTest extends Specification {
protected static PostgreSQLContainer postgres = new PostgreSQLContainer()
.withDatabaseName("db")
.withUsername("root")
.withPassword("root")
def setupSpec() {
startPostgresIfNeeded()
['spring.datasource.url' : postgres.getJdbcUrl(),
'spring.datasource.username': postgres.getUsername(),
'spring.datasource.password': postgres.getPassword()
].each { k, v ->
System.setProperty(k, v)
}
}
private static void startPostgresIfNeeded() {
if (!postgres.isRunning()) {
log.info("[BASE-INTEGRATION-TEST] - Postgres is not started. Running...")
postgres.start()
}
}
def cleanupSpec() {
if (postgres.isRunning()) {
log.info("[BASE-INTEGRATION-TEST] - Stopping Postgres...")
postgres.stop()
}
}
}
UserRepositoryIntSpec
class UserRepositoryIntSpec extends BaseIntegrationTest {
@Autowired
private UserRepository UserRepository
def setup() {
UserRepository.deleteAll()
}
def "FindAll returns all users correctly"() {
given:
List<Integer> friends = [1,2]
User User1 = User.builder()
.riskcustomerid(1)
.possibleids([1000, 1001])
.preferableid(1000)
.totalfriendscount(2)
.friends(friends)
.build()
User User2 = User.builder()
.riskcustomerid(2)
.possibleids([8000, 8001])
.preferableid(8000)
.totalfriendscount(3)
.friends(friends)
.build()
when:
UserRepository.saveAll([User1, User2])
then:
List<User> Users = UserRepository.findAll()
assert Users.size() == 2
User user1 = Users.get(0)
User user2 = Users.get(1)
assert user1.getId() == 1 && user1.getRiskcustomerid() == 1 && user1.getDateCreated() != null
assert user2.getId() == 2 && user2.getRiskcustomerid() == 2 && user2.getDateCreated() != null
}
我看到您正在使用
url:jdbc:postgresql://localhost:5432/db
。你真的是在说“请与我的本地DB竞争”:
对于您的用例,我建议在Testcontainers中使用。它将自动启动容器,并在您关闭与它的最后一个连接时销毁它。我看到您正在使用
url:jdbc:postgresql://localhost:5432/db
。你真的是在说“请与我的本地DB竞争”:
对于您的用例,我建议在Testcontainers中使用。它将自动启动容器,并在您关闭上次连接时将其销毁。id与计数有什么关系?另外,通过持久性测试断言ID通常是个坏主意。。如果您有多个测试,那么序列会增加更多。这就是为什么我会在每次测试之前运行设置块。但我也不明白为什么每次都在本地数据库中清除用户表,因为我希望测试在容器中用新创建的数据库运行?删除数据不会重置序列。不要假设ID是一个,而是假设有一个。虽然有什么用你不相信尤伯纳特吗?如果它是我们保存的,它有一个id。好吧,让我们忘记计数。但是为什么使用本地数据库而不是容器数据库呢?为什么本地表是清晰的和使用的,而不是容器表?我假设,事实上,每次我向本地数据库添加信息时,它实际上会在测试后被删除,因为
setupSpec
是在应用程序启动后执行的。id与计数有什么关系?另外,通过持久性测试断言ID通常是个坏主意。。如果您有多个测试,那么序列会增加更多。这就是为什么我会在每次测试之前运行设置块。但我也不明白为什么每次都在本地数据库中清除用户表,因为我希望测试在容器中用新创建的数据库运行?删除数据不会重置序列。不要假设ID是一个,而是假设有一个。虽然有什么用你不相信尤伯纳特吗?如果它是我们保存的,它有一个id。好吧,让我们忘记计数。但是为什么使用本地数据库而不是容器数据库呢?为什么本地表是清晰的和使用的,而不是容器表?我假设,事实上,每次我向本地数据库添加信息时,它实际上会在测试后被删除,因为setupSpec
是在应用程序启动后执行的。是的,到底是什么问题。谢谢是的,到底是什么问题。谢谢
spring:
datasource:
url: jdbc:postgresql://localhost:5432/db
username: root
password: root
hikari:
connection-timeout: 10000
leak-detection-threshold: 60000
validation-timeout: 30000
connection-test-query: SELECT 1;
jdbc-url: jdbc:postgresql://localhost:5432/db
username: root
password: root
data-source-properties: stringtype=unspecified
maximum-pool-size: 16
max-lifetime: 1800000
transaction-isolation: TRANSACTION_READ_COMMITTED
pool-name: hikari.local
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: update
flyway:
schemas: schema1
baseline-on-migrate: false
server:
port: 8080