admin管理员组

文章数量:1289900

I am trying to test a spring boot application using dapr. For my test set up I want to start a Dapr testcontainer and have a Conmponent that references a Wiremock testcontainer to stub http calls.

class TestcontainersConfiguration {

    private static final Network network = Network.newNetwork();

    @Bean
    @ServiceConnection
    public DaprContainer daprContainer(WireMockContainer wireMockContainer) {
        // Get the network alias instead of the base URL
        String wireServiceUrl = "http://" + wireMockContainer.getNetworkAliases().getFirst() + ":" + wireMockContainer.getPort();

        DaprContainer dapr = new DaprContainer("daprio/daprd:latest")
                .withAppName("local-dapr-app")
                .withNetwork(network)
                .withComponent(new Component("warehouse", "http", "v1", Collections.singletonList(new MetadataEntry("url", wireServiceUrl))))
                .withAppPort(8080)
                .withAppChannelAddress("host.testcontainers.internal");
        return dapr;
    }


    @Bean
    WireMockContainer wireMockContainer() {
        WireMockContainer wireMockContainer = new WireMockContainer("wiremock/wiremock:3.12.0-1")
                .withMappingFromResource("warehouse-service-stubs.json")
                .withNetwork(network);
        wireMockContainer.start();
        return wireMockContainer;
    }

    @Bean
    public DynamicPropertyRegistrar wireMockProperties(WireMockContainer wireMockContainer, DaprContainer dapr) {
        return registry -> {
            registry.add("DAPR_HTTP_ENDPOINT", wireMockContainer::getBaseUrl);
            registry.add("dapr.grpc.port", () -> dapr.getMappedPort(50001));
            registry.add("dapr.http.port", () -> dapr.getMappedPort(3500));
        };
    }
}

If I start the set up with the .withComponent line, the health check of the dapr container fails. If i remove it, the application fails because there is no component with the id "warehouse". Is there a working set up to stub http calls in tests with dapr?

I am trying to test a spring boot application using dapr. For my test set up I want to start a Dapr testcontainer and have a Conmponent that references a Wiremock testcontainer to stub http calls.

class TestcontainersConfiguration {

    private static final Network network = Network.newNetwork();

    @Bean
    @ServiceConnection
    public DaprContainer daprContainer(WireMockContainer wireMockContainer) {
        // Get the network alias instead of the base URL
        String wireServiceUrl = "http://" + wireMockContainer.getNetworkAliases().getFirst() + ":" + wireMockContainer.getPort();

        DaprContainer dapr = new DaprContainer("daprio/daprd:latest")
                .withAppName("local-dapr-app")
                .withNetwork(network)
                .withComponent(new Component("warehouse", "http", "v1", Collections.singletonList(new MetadataEntry("url", wireServiceUrl))))
                .withAppPort(8080)
                .withAppChannelAddress("host.testcontainers.internal");
        return dapr;
    }


    @Bean
    WireMockContainer wireMockContainer() {
        WireMockContainer wireMockContainer = new WireMockContainer("wiremock/wiremock:3.12.0-1")
                .withMappingFromResource("warehouse-service-stubs.json")
                .withNetwork(network);
        wireMockContainer.start();
        return wireMockContainer;
    }

    @Bean
    public DynamicPropertyRegistrar wireMockProperties(WireMockContainer wireMockContainer, DaprContainer dapr) {
        return registry -> {
            registry.add("DAPR_HTTP_ENDPOINT", wireMockContainer::getBaseUrl);
            registry.add("dapr.grpc.port", () -> dapr.getMappedPort(50001));
            registry.add("dapr.http.port", () -> dapr.getMappedPort(3500));
        };
    }
}

If I start the set up with the .withComponent line, the health check of the dapr container fails. If i remove it, the application fails because there is no component with the id "warehouse". Is there a working set up to stub http calls in tests with dapr?

Share Improve this question edited Feb 20 at 12:59 JP13 asked Feb 20 at 12:42 JP13JP13 701 silver badge9 bronze badges 1
  • If I start the set up with the .withComponent line, the health check of the dapr container fails. ... It fails how? What is the error? What is the URL for the wiremock container. – M. Deinum Commented Feb 20 at 14:23
Add a comment  | 

1 Answer 1

Reset to default 1

thanks for asking this question, this helps us to refine the Dapr and Spring Boot integration. Is there any way you can share your code in GitHub for me to take a look at it? I think we can definitely add a test upstream to demonstrate how to configure this scenario, but we need to look at the code a little bit closer (errors, and how you are calling the service).

Please create an issue in the https://github/dapr/java-sdk/ repository so we can keep track of this.

If you have time, you can also check this PR, which we are working with the folks from https://microcks.io to cover more complex testing scenarios: https://github/salaboy/pizza/pull/13/files

本文标签: