vaadin / quarkus

An extension to Quarkus to support Vaadin Flow
Apache License 2.0
28 stars 3 forks source link

Quarkus Test and TestBench UI Unit Testing don't work together when Vaadin routes use CDI #140

Closed mshabarov closed 4 months ago

mshabarov commented 7 months ago

CDI injection doesn't work in test environment when TestBench UI Unit Testing is used in Quarkus Test. Suppose the following configuration:

Vaadin view with CDI

@Route
class ReportView extends VerticalLayout {
   public ReportView(GreetService greetService) { 
        add(new Span(greetService.greet()));
    }
}

Service

@ApplicationScoped
public class GreetService {
    public String greet() {
        return "hello"
    }
}

Quarkus test

@QuarkusTest
class ReportViewTest extends UIUnitTest {
    @Test
    void test() {
        // GreetService isn't injected
        final ReportView reportView = navigate(ReportView.class);
    }
}

The reason for this is that MockVaadin doesn't know anything about QuarkusInstantiator and doesn't take into account BeanManager.

Quarkus add-on should initialise UI Unit testing mock to allow CDI injections.

SpringUIUnitTest and UITestSpringLookupInitializer are the examples of DI framework integration, similar integration can be made for Quarkus.

More context and references:

  1. See original ticket where this problem was reported https://github.com/vaadin/quarkus/issues/121
  2. Workarounds https://github.com/vaadin/quarkus/issues/121#issuecomment-1636061563 and https://github.com/AliceAndTheBuilders/pro-starter-flow-quarkus/blob/32f23c27e1509d016002b5032f3d4d14f2d0df97/src/test/java/com/example/starter/pro/UIUnitTestHelper.java#L120