Micronaut+ vertx+测试范围

发布于 2025-01-23 03:54:56 字数 563 浏览 0 评论 0原文

如何使用Vert.x和TestContainers配置Micronaut应用?我正在尝试:

application-test.yml

datasources:
  default:
    url: jdbc:tc:mysql:8:///db
    driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
vertx:
  mysql:
    client:
      uri: jdbc:tc:mysql:8:///db

使用micronaut-data-jdbc进行测试,但使用micronaut-vertx-mysql-client不使用工作:

错误:

消息:无法解析无效连接URI:JDBC:TC:MySQL:8:/// DB

我对TesteContainers并不熟悉,但似乎并没有出现使用固定端口,所以我不知道如何配置连接URI。

谢谢!

How do I configure Micronaut app using Vert.x and testcontainers? I'm trying:

application-test.yml

datasources:
  default:
    url: jdbc:tc:mysql:8:///db
    driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver
vertx:
  mysql:
    client:
      uri: jdbc:tc:mysql:8:///db

Tests with micronaut-data-jdbc work, but with micronaut-vertx-mysql-client not work:

Error:

Message: Cannot parse invalid connection URI: jdbc:tc:mysql:8:///db

I'm not very familiar with testecontainers, but it seems like it doesn't come up with a fixed port, so I don't know how to configure the connection URI.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

动次打次papapa 2025-01-30 03:54:56

micronaut-vertx-mysql-client不支持TestContainers JDBC URL方案(如果没有其他日志),这可能是一个问题。

在这种情况下,我建议使用而不是特殊的JDBC URL。

It might be a problem that micronaut-vertx-mysql-client does not support the Testcontainers JDBC URL scheme (hard to say without further logs).

In this case, I would suggest to use Testcontainers with database container objects instead of the special JDBC URL.

茶花眉 2025-01-30 03:54:56

我有一个解决方案:

Micronaut + JDBC Hikari + Vertx mysql客户端 + Flyway mysql

package br.com.app;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.PropertySource;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.HttpClient;
import io.micronaut.runtime.EmbeddedApplication;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
class AuthTest {

 static Logger log = LoggerFactory.getLogger(AuthTest.class.getName());

    @Container
    static MySQLContainer mysql = new MySQLContainer("mysql:8");

    private HttpClient client;
    private static EmbeddedApplication application;
    private static ApplicationContext context;

    @BeforeAll
    public static void initTests(){

        log.info("Mysql is running {}, port {}", mysql.isRunning(), mysql.getFirstMappedPort());

        var port = mysql.getFirstMappedPort();
        var host = mysql.getHost();
        var database = mysql.getDatabaseName();
        var user = mysql.getUsername();
        var password = mysql.getPassword();
        var url = String.format("jdbc:mysql://%s:%s/%s", host, port, database);

        application = ApplicationContext.run(EmbeddedApplication.class,
                PropertySource.of(
                        "test",
                        CollectionUtils.mapOf(
                            "vertx.mysql.client.port", port,
                            "vertx.mysql.client.host", host,
                            "vertx.mysql.client.database", database,
                            "vertx.mysql.client.user", user,
                            "vertx.mysql.client.password", password,
                            "datasources.default.url", url,
                            "datasources.default.username", user,
                            "datasources.default.password", password,
                            "flyway.datasources.default.enabled", true
                        )
                ));
        context = application.getApplicationContext();
    }

    @BeforeEach
    void beforeEach(){
        this.authService = context.getBean(AuthService.class);
        this.client = context.getBean(HttpClient.class);
    }    

    @Test
    void testItWorks() {
        Assertions.assertTrue(application.isRunning());
    }

   // api tests
}

帮助链接:

https://dev.to/major13ua/micronaut-integration-testing-testing-using-testcontainers-2e30

https://github.com/major.com/major13ua/micronaut-tc/micronaut-tc/blob/blob/main/main/src/src/test。 /java/com/example/testcontainer/controller/democontrollertest.java

I got a solution to the problem:

Micronaut + jdbc hikari + vertx mysql client + flyway mysql

package br.com.app;

import io.micronaut.context.ApplicationContext;
import io.micronaut.context.env.PropertySource;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.client.HttpClient;
import io.micronaut.runtime.EmbeddedApplication;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@Testcontainers
class AuthTest {

 static Logger log = LoggerFactory.getLogger(AuthTest.class.getName());

    @Container
    static MySQLContainer mysql = new MySQLContainer("mysql:8");

    private HttpClient client;
    private static EmbeddedApplication application;
    private static ApplicationContext context;

    @BeforeAll
    public static void initTests(){

        log.info("Mysql is running {}, port {}", mysql.isRunning(), mysql.getFirstMappedPort());

        var port = mysql.getFirstMappedPort();
        var host = mysql.getHost();
        var database = mysql.getDatabaseName();
        var user = mysql.getUsername();
        var password = mysql.getPassword();
        var url = String.format("jdbc:mysql://%s:%s/%s", host, port, database);

        application = ApplicationContext.run(EmbeddedApplication.class,
                PropertySource.of(
                        "test",
                        CollectionUtils.mapOf(
                            "vertx.mysql.client.port", port,
                            "vertx.mysql.client.host", host,
                            "vertx.mysql.client.database", database,
                            "vertx.mysql.client.user", user,
                            "vertx.mysql.client.password", password,
                            "datasources.default.url", url,
                            "datasources.default.username", user,
                            "datasources.default.password", password,
                            "flyway.datasources.default.enabled", true
                        )
                ));
        context = application.getApplicationContext();
    }

    @BeforeEach
    void beforeEach(){
        this.authService = context.getBean(AuthService.class);
        this.client = context.getBean(HttpClient.class);
    }    

    @Test
    void testItWorks() {
        Assertions.assertTrue(application.isRunning());
    }

   // api tests
}

Help links:

https://dev.to/major13ua/micronaut-integration-testing-using-testcontainers-2e30

https://github.com/major13ua/micronaut-tc/blob/main/src/test/java/com/example/testcontainer/controller/DemoControllerTest.java

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文