Добавил интеграционные тесты JDBC-хранилища счетов с PostgreSQL Testcontainers
This commit is contained in:
1 parent
299acbe554
commit
6acbabf6f2
2 files changed
+93
No files matched your search
@@ -59,6 +59,24 @@
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-database-postgresql</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-testcontainers</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers-junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers-postgresql</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package net.sytes.kashey.easymoney.persistence;
|
||||
|
||||
import model.Account;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.testcontainers.postgresql.PostgreSQLContainer;
|
||||
import service.AccountStore;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@Testcontainers
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
|
||||
@Transactional
|
||||
class AccountRepositoryTest {
|
||||
|
||||
@Container
|
||||
@ServiceConnection
|
||||
static PostgreSQLContainer postgres =
|
||||
new PostgreSQLContainer("postgres:17");
|
||||
|
||||
@Autowired
|
||||
private AccountStore accountStore;
|
||||
|
||||
@Test
|
||||
void creatingAccountPersistsNameAndGeneratesId() {
|
||||
// When
|
||||
Account created = accountStore.create("Карта");
|
||||
|
||||
// Then
|
||||
assertNotNull(created.getId(), "База должна назначить ID");
|
||||
assertEquals("Карта", created.getName());
|
||||
|
||||
Account loaded = accountStore.findById(created.getId());
|
||||
|
||||
assertNotNull(loaded, "Созданный счёт должен находиться в БД");
|
||||
assertEquals(created.getId(), loaded.getId());
|
||||
assertEquals("Карта", loaded.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void findingUnknownAccountReturnsNull() {
|
||||
// When
|
||||
Account account = accountStore.findById(-1L);
|
||||
|
||||
// Then
|
||||
assertNull(account, "Для отсутствующего ID должен возвращаться null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void creatingTwoAccountsPersistsNamesAndGeneratesDifferentIds() {
|
||||
// When
|
||||
Account card = accountStore.create("Карта");
|
||||
Account cash = accountStore.create("Наличные");
|
||||
|
||||
// Then
|
||||
Account loadedCard = accountStore.findById(card.getId());
|
||||
Account loadedCash = accountStore.findById(cash.getId());
|
||||
|
||||
assertNotNull(loadedCard);
|
||||
assertNotNull(loadedCash);
|
||||
|
||||
assertEquals("Карта", loadedCard.getName());
|
||||
assertEquals("Наличные", loadedCash.getName());
|
||||
|
||||
assertEquals(card, loadedCard);
|
||||
assertEquals(cash, loadedCash);
|
||||
|
||||
assertNotEquals(card.getId(), cash.getId(), "Id созданных счетов должны различаться");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user