CDI Event Arrival Integration Test with Quarkus

To test the arrival of a CDI Event sent from a managed bean:


import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Event;
import javax.inject.Inject;

@ApplicationScoped
public class MessageSender {

    @Inject
    Event<String> messageEvent;

    public void send(){
        this.messageEvent.fire("duke rocks!");
    }
}

...you can inject the managed bean into an integration test with ordered methods as "Class under Test":


import static org.junit.jupiter.api.Assertions.assertEquals;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import io.quarkus.test.junit.QuarkusTest;


@QuarkusTest
@ApplicationScoped
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class MessageSenderIT {
    
    @Inject
    MessageSender cut;
    
    String expected;

    //...the first test sends the event:
    @Test
    @Order(1)
    void send() {
        this.cut.send();     
    }

    //on "ordinary" CDI listener receives the message
    public void receive(@Observes String message){
        this.expected = message;
    }

    //the last test method verifies the arrival of the event
    @Test
    @Order(2)
    void verifyArrival() {
        assertEquals("duke rocks!",this.expected);
    }
}

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments
License