application-test.properties
Код: Выделить всё
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.jdbc.batch_size=0
Код: Выделить всё
@Entity
@Getter
@Setter
public class Dog {
@Id
@SequenceGenerator(name = "dog_sequence",
sequenceName = "dog_sequence",
allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "dog_sequence")
private Long id;
@Column
private String name;
@Column(columnDefinition = "TEXT")
private String description;
}
Код: Выделить всё
public interface DogRepository extends JpaRepository {}
Код: Выделить всё
@Testcontainers
@DataJpaTest
@ActiveProfiles("test")
public class DogTest {
@Container
static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:15.4")
.withDatabaseName("testdb")
.withUsername("postgres")
.withPassword("postgres");
@DynamicPropertySource
static void overrideProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgres::getJdbcUrl);
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
registry.add("spring.datasource.driver-class-name", postgres::getDriverClassName);
registry.add("spring.jpa.hibernate.ddl-auto", () -> "create-drop");
registry.add("spring.jpa.database-platform", () -> "org.hibernate.dialect.PostgreSQLDialect");
}
@Autowired
private DogRepository repository;
@Autowired
private TestEntityManager entityManager;
@Before
public void setUp() {
for (int i = 0; i < 10; i++) {
Dog dog = new Dog();
entityManager.persist(dog);
}
entityManager.flush();
}
@Test
public void testFindDogs() {
List dogs = repository.findAll();
Assertions.assertEquals(1, dogs.size());
}
}
Код: Выделить всё
Hibernate:
create table "dog" (
"id" bigint not null,
"description" "TEXT",
"name" varchar(255),
primary key ("id")
)
2025-12-04T20:27:04.096-05:00 WARN 560907 --- [ main] o.h.t.s.i.ExceptionHandlerLoggedImpl : GenerationTarget encountered exception accepting command : Error executing DDL "
create table "dog" (
"id" bigint not null,
"description" "TEXT",
"name" varchar(255),
primary key ("id")
)" via JDBC [Domain "TEXT" not found;]
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "
create table "dog" (
"id" bigint not null,
"description" "TEXT",
"name" varchar(255),
primary key ("id")
)" via JDBC [Domain "TEXT" not found;]
Как запустить тест без удаления типа столбца TEXT?
Подробнее здесь: https://stackoverflow.com/questions/798 ... -not-found
Мобильная версия