citrusframework / citrus

Framework for automated integration tests with focus on messaging integration
https://citrusframework.org
Apache License 2.0
445 stars 135 forks source link

Junit parallel test #1137

Closed TheEnjoy closed 3 months ago

TheEnjoy commented 3 months ago

Citrus Version 4.1.0

Question How do I run api tests in parallel? I want to run a parallel test for JUnit, I use a special property

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = concurrent

My code

  1. Send request

    $.run(http().client(client)
                .send()
                .get("/api/fruits"+randomValue));
  2. Server receive

    $.run(http().server(server)
                .receive()
                .get()
                .validate((message, testContext) -> {
                    System.out.println(message);
                    if(message.getHeaders().get("citrus_endpoint_uri").toString().contains(randomValue)){
                        receiveOK2 = true;
                    }
                })
    
        );
    1. Send response for client
    if (receiveOK2) {
            $.run(http().server(server)
                    .send()
                    .response(HttpStatus.OK)
                    .message()
                    .body("{ \"name\": \"" + randomValue + "\", \"value\": \"1\" }"));
        }

Problem: For step 2 i have problem. For second thread in mockServer citrus not see client request.

No message received with message selector - retrying in 500ms

I think problem in scope test, when i run parallel test, all requset sends in test1 scope.

i`m check Test1: context.messageStore() - 2 request Test2: context.messageStore() - 0 request (Error: No message received with message selector - retrying in 500ms)

  @Test
    @CitrusTest
    public void test2(@CitrusResource TestActionRunner $) 
   @Test
    @CitrusTest
    public void test1(@CitrusResource TestActionRunner $) {

I can create Spring Context for all test, but will it be correct?

TheEnjoy commented 3 months ago

@bbortt Hi there, do you have any ideas for a parallel run test that does not require the use of a dynamic adapter?

christophd commented 3 months ago

You need to specify a message selector for step 2 on the receive operation. something like:

$.run(http().server(server)
       .receive()
       .get()
       .selector("citrus_http_request_uri = '/api/fruits"+randomValue+"'")
       .validate((message, testContext) -> {
             System.out.println(message);
             if(message.getHeaders().get("citrus_endpoint_uri").toString().contains(randomValue)){
                receiveOK2 = true;
             }
        })
);
TheEnjoy commented 3 months ago

@christophd Thanks, we try https://github.com/citrusframework/citrus/issues/550#issuecomment-445818626 Will be retry

TheEnjoy commented 3 months ago

Resolved, selector is work. All work