Код: Выделить всё
@MyTestConfig
class MyTests {
@Autowired SomeJpaRepo repo;
@Test
void findAll() {
assertThatCode(repo::findAll).doesNotThrowAnyExceptions();
}
}
< /code>
То, что я начал, было сделать аннотацию < /p>
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestContainers
public @interface MyTestConfig {
}
< /code>
С помощью этого следующего тестирования@MyTestConfig
class MyTests {
@Container
private static final MySQLContainer MYSQL_CONTAINER =
new MySQLContainer(
DockerImageName.parse("library/mariadb")
.asCompatibleSubstituteFor("mysql"));
@DynamicPropertySource
static void mysqlProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", MYSQL_CONTAINER::getJdbcUrl);
registry.add("spring.datasource.username", MYSQL_CONTAINER::getUsername);
registry.add("spring.datasource.password", MYSQL_CONTAINER::getPassword);
}
@Autowired SomeJpaRepo repo;
@Test
void findAll() {
assertThatCode(repo::findAll).doesNotThrowAnyExceptions();
}
}
Код: Выделить всё
abstract class AbstractMyTest {
@Container
private static final MySQLContainer MYSQL_CONTAINER =
new MySQLContainer(
DockerImageName.parse("library/mariadb")
.asCompatibleSubstituteFor("mysql"));
@DynamicPropertySource
static void mysqlProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", MYSQL_CONTAINER::getJdbcUrl);
registry.add("spring.datasource.username", MYSQL_CONTAINER::getUsername);
registry.add("spring.datasource.password", MYSQL_CONTAINER::getPassword);
}
}
Подробнее здесь: https://stackoverflow.com/questions/794 ... -i-can-use