小编sky*_*yho的帖子

是否可以并行运行集成测试(Spring boot)?

已经为特定应用程序配置了测试环境(几十个测试类)。

这里也使用了TestContainer 。

我看到这样的算法

  1. 在整个测试过程中,应用程序上下文只会引发一次。
  2. 还使用Postgresql引发一个容器。
  3. 由于不可能遵循集成测试的顺序,因此每个测试都会自行清理写入数据库的数据。
  4. 从JVM中找出分配给它的处理器核心数,并根据这个数字为线程池创建容量。
  5. 在具有应用程序上下文执行器...)的类中创建一个线程池,并发出为线程运行测试类的任务。
  • 是否可以控制测试类的启动,即如何从代码中做到这一点?
  1. 要同时运行多个线程(根据当前系统的核心数量),我们可以使用CyclicBarrier
  2. 或者您可以使用不同的方法:

同样,您需要知道如何使用测试方法从类的对象运行此类的所有测试方法。

因此,我们创建测试类的对象,并使用Stream API,然后调用parallel()方法(但这里我不控制同时运行的线程数)。

解决这个问题有哪些选择?

谁有什么想法?

java concurrency integration-testing spring-boot

12
推荐指数
1
解决办法
1万
查看次数

使用 Testcontainers 时如何设置 Postgresql 的端口?

有时我需要为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)

postgresql spring-boot testcontainers java-11

10
推荐指数
1
解决办法
8678
查看次数

基于角色的 Spring Boot Keycloak 不起作用,这些角色在 keycloak 中分配给用户

我已经配置了Keycloak的使用,而不使用弹簧适配器。因为它已被弃用。我在Keycloak的控制台中创建了:一个REALM,一个用户,并为用户添加角色。

在此输入图像描述

在此输入图像描述

  • user 然后我创建了一个用户并向他添加了我之前在此处创建的角色。

在此输入图像描述

在此输入图像描述

  • 泊坞窗
....
  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)

这是应用程序配置

  • pom.xml
  <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)

spring-boot keycloak

7
推荐指数
1
解决办法
5411
查看次数

TestContainers 和错误:“无法验证连接 org.postgresql.jdbc.PgConnection”(为所有测试类引发单个容器)

当我尝试逐一运行测试时遇到问题。\n数据库连接已关闭。

\n

根据文档(声明为静态字段的容器...),我试图确保我的容器在所有测试中都被引发一次。

\n

我专门使用它来获得Spring应用程序上下文和一次引发并用于所有测试的测试容器。

\n

确实如此,因为我在每个测试中都会进行检查:

\n
      boolean running = getPostgreSQLContainer().isRunning();\n\n        System.out.println(running);\n
Run Code Online (Sandbox Code Playgroud)\n

也就是说,测试是一项一项自动运行的。

\n
    \n
  • pom.xml
  • \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

5
推荐指数
1
解决办法
1万
查看次数

尽管目标类实现了该接口,为什么 Spring AOP 仍使用 CGLIB?

\n

Spring AOP是一个基于代理的AOP框架。这意味着要实现目标对象的方面,它将创建该对象的代理。这是通过以下两种方式之一实现的:

\n
    \n
  • JDK动态代理\xe2\x80\x93是Spring AOP的首选方式。每当目标对象实现一个接口时,就会使用 JDK 动态代理
  • \n
  • CGLIB代理\xe2\x80\x93 如果目标对象没有实现接口,则可以使用CGLIB代理
  • \n
\n
\n

来源

\n

然而,CGLIB 总是被使用...这既适用于标准 Spring 交付(例如,使用 @Transactional 注释),也适用于使用其书面方面

\n
public interface MyService {\n\n    void methodFirst();\n\n    void methodSecond();\n}\n\n
Run 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\n
Run 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    }\n
Run 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)

java spring spring-aop spring-boot java-11

5
推荐指数
1
解决办法
1427
查看次数

Spring boot + slf4j + log4j + 类 org.apache.logging.slf4j.SLF4JLoggerContext 无法转换为类

我有Spring boot 2.3.1

  • pom.xml

<?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)

log4j spring-boot slf4j-api java-11

2
推荐指数
1
解决办法
8615
查看次数

Kafka CLI 工具在 wurstmeister/kafka 中的位置

我已经将 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 映像中?

apache-kafka docker

1
推荐指数
1
解决办法
1415
查看次数