已经为特定应用程序配置了测试环境(几十个测试类)。
这里也使用了TestContainer 。
我看到这样的算法:
同样,您需要知道如何使用测试方法从类的对象运行此类的所有测试方法。
因此,我们创建测试类的对象,并使用Stream API,然后调用parallel()方法(但这里我不控制同时运行的线程数)。
解决这个问题有哪些选择?
谁有什么想法?
有时我需要为Postgresql安装一个端口,我在容器中运行该端口进行测试。但测试容器开发者命令Testcontainers删除了这个功能。但在某个地方有一个解决方案,通过设置,但我找不到它。谁有关于如何做到这一点的任何想法或信息?
public class ContainerConfig {
private static final PostgreSQLContainer postgresSQLContainer;
static {
DockerImageName postgres = DockerImageName.parse("postgres:13.1");
postgresSQLContainer = (PostgreSQLContainer) new PostgreSQLContainer(postgres)
.withDatabaseName("test")
.withUsername("root")
.withPassword("root")
.withReuse(true);
postgresSQLContainer.start();
}
@SuppressWarnings("rawtypes")
private static PostgreSQLContainer getPostgresSQLContainer() {
return postgresSQLContainer;
}
@SuppressWarnings("unused")
@DynamicPropertySource
public static void registerPgProperties(DynamicPropertyRegistry propertyRegistry) {
propertyRegistry.add("integration-tests-db", getPostgresSQLContainer()::getDatabaseName);
propertyRegistry.add("spring.datasource.username", getPostgresSQLContainer()::getUsername);
propertyRegistry.add("spring.datasource.password", getPostgresSQLContainer()::getPassword);
propertyRegistry.add("spring.datasource.url", getPostgresSQLContainer()::getJdbcUrl);
}
}
Run Code Online (Sandbox Code Playgroud) 我已经配置了Keycloak的使用,而不使用弹簧适配器。因为它已被弃用。我在Keycloak的控制台中创建了:一个REALM,一个用户,并为用户添加角色。
....
keycloak:
container_name: blog
depends_on:
keycloakdb:
condition: service_healthy
environment:
DB_DATABASE: ${POSTGRESQL_DB}
DB_USER: ${POSTGRESQL_USER}
DB_PASSWORD: ${POSTGRESQL_PASS}
KEYCLOAK_USER: ${KEYCLOAK_USER}
KEYCLOAK_PASSWORD: ${KEYCLOAK_PASSWORD}
DB_VENDOR: ${DB_VENDOR}
DB_ADDR: ${DB_ADDR}
DEBUG_PORT: ${DEBUG_PORT}
DB_PORT: ${DB_PORT}
TZ: ${TZ}
DEBUG: ${DEBUG}
image: jboss/keycloak:latest
.....
Run Code Online (Sandbox Code Playgroud)
这是应用程序配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.guide</groupId>
<artifactId>keycloak-postgres-quick-guide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>keycloak-postgres-quick-guide</name>
<description>keycloak-postgres-quick-guide</description>
<properties>
<java.version>17</java.version>
<testcontainers.version>1.17.6</testcontainers.version>
<snakeyaml.version>1.33</snakeyaml.version>
<keycloak.version>20.0.2</keycloak.version>
</properties>
<dependencies>
....
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId> …Run Code Online (Sandbox Code Playgroud) 当我尝试逐一运行测试时遇到问题。\n数据库连接已关闭。
\n根据文档(声明为静态字段的容器...),我试图确保我的容器在所有测试中都被引发一次。
\n我专门使用它来获得Spring的应用程序上下文和一次引发并用于所有测试的测试容器。
\n确实如此,因为我在每个测试中都会进行检查:
\n boolean running = getPostgreSQLContainer().isRunning();\n\n System.out.println(running);\nRun Code Online (Sandbox Code Playgroud)\n也就是说,测试是一项一项自动运行的。
\n <parent>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-parent</artifactId>\n <version>2.4.1</version>\n <relativePath/> <!-- lookup parent from repository -->\n </parent>\n\n....\n\n<properties>\n <java.version>11</java.version>\n <testcontainers.version>1.15.1</testcontainers.version>\n <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>\n <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>\n <version.mapstruct>1.4.1.Final</version.mapstruct>\n <version.maven.compiler.plugin>3.8.1</version.maven.compiler.plugin>\n <version.embedded.postgresql.testcontainers>1.86</version.embedded.postgresql.testcontainers>\n <version.spring.cloud.starter>2.2.6.RELEASE</version.spring.cloud.starter>\n </properties>\n\n <dependencies>\n <dependency>\n <groupId>org.testcontainers</groupId>\n <artifactId>postgresql</artifactId>\n <scope>test</scope>\n </dependency>\n\n <dependency>\n <groupId>org.mapstruct</groupId>\n <artifactId>mapstruct</artifactId>\n <version>${version.mapstruct}</version>\n </dependency>\n\n <dependency>\n <groupId>org.mapstruct</groupId>\n <artifactId>mapstruct-processor</artifactId>\n <version>${version.mapstruct}</version>\n <scope>provided</scope>\n </dependency>\n\n <dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-data-jpa</artifactId>\n </dependency>\n <dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-data-rest</artifactId>\n </dependency>\n <dependency>\n <groupId>org.springframework.boot</groupId>\n <artifactId>spring-boot-starter-web</artifactId>\n </dependency>\n\n <dependency>\n <groupId>org.liquibase</groupId>\n <artifactId>liquibase-core</artifactId>\n …Run Code Online (Sandbox Code Playgroud) postgresql integration-testing docker spring-boot testcontainers
\n\n\nSpring AOP是一个基于代理的AOP框架。这意味着要实现目标对象的方面,它将创建该对象的代理。这是通过以下两种方式之一实现的:
\n\n
\n- JDK动态代理\xe2\x80\x93是Spring AOP的首选方式。每当目标对象实现一个接口时,就会使用 JDK 动态代理
\n- CGLIB代理\xe2\x80\x93 如果目标对象没有实现接口,则可以使用CGLIB代理
\n
然而,CGLIB 总是被使用...这既适用于标准 Spring 交付(例如,使用 @Transactional 注释),也适用于使用其书面方面
\npublic interface MyService {\n\n void methodFirst();\n\n void methodSecond();\n}\n\nRun Code Online (Sandbox Code Playgroud)\n@Service\npublic class MyServiceImpl implements MyService {\n\n @Override\n @AnnotationCustom\n public void methodFirst() {\n System.out.println("methodFirst()");\n methodSecond();\n }\n\n @Override\n @AnnotationCustom\n public void methodSecond() {\n System.out.println("methodSecond()");\n System.out.println();\n }\n\nRun Code Online (Sandbox Code Playgroud)\n@SpringBootApplication\npublic class AopTransactionalSpringApplication {\n\n public static void main(String[] args) {\n SpringApplication.run(AopTransactionalSpringApplication.class, args);\n }\nRun Code Online (Sandbox Code Playgroud)\n@Aspect\n@Component\npublic class AspectCustom {\n\n @Pointcut("execution(public * *(..))")\n public …Run Code Online (Sandbox Code Playgroud) 我有Spring boot 2.3.1
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>logger</groupId>
<artifactId>logger</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>logger</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<lombok.version>1.18.12</lombok.version>
<log4j.version>1.2.17</log4j.version>
<slf4j.version>1.7.30</slf4j.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> …Run Code Online (Sandbox Code Playgroud) 我已经将 kafka 部署在 docker 容器中。
version: '3.8'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
kafka:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'false'
Run Code Online (Sandbox Code Playgroud)
我在其中一个视频中看到 kafka 工具包存在于沿途的发行版中
/usr/bin
kafka 工具包不在该图像中的该位置
也许这个集合可以以某种方式安装,或者它是否存在于任何其他 kafka 映像中?
spring-boot ×6
java-11 ×3
docker ×2
java ×2
postgresql ×2
apache-kafka ×1
concurrency ×1
keycloak ×1
log4j ×1
slf4j-api ×1
spring ×1
spring-aop ×1