mvysny / karibu-testing

Vaadin Server-Side Browserless Containerless Unit Testing
Apache License 2.0
105 stars 14 forks source link

Error when using @RouteScope in components - version 1.3.18 (and also 1.3.17) #129

Open brunovianarezende opened 1 year ago

brunovianarezende commented 1 year ago

I have a component annotated with @RouteScope and when I try to init the app in the test, I get the error:

Caused by: java.lang.IllegalStateException: UI bean store is not found by the initial UI id via the key 'uid-1' and it's not found by the key 'win-ROOT-2521314-0.2626611481' after relocation.
    at com.vaadin.flow.spring.scopes.VaadinRouteScope$RouteStoreWrapper.relocateStore(VaadinRouteScope.java:123)
    at com.vaadin.flow.spring.scopes.VaadinRouteScope$RouteStoreWrapper.lambda$getBeanStore$b500751e$1(VaadinRouteScope.java:100)
    at com.vaadin.flow.component.page.Page.retrieveExtendedClientDetails(Page.java:567)
    at com.github.mvysny.kaributesting.v10.MockPage.retrieveExtendedClientDetails(MockVaadin.kt:421)
    at com.vaadin.flow.spring.scopes.VaadinRouteScope$RouteStoreWrapper.getBeanStore(VaadinRouteScope.java:99)
    at com.vaadin.flow.spring.scopes.VaadinRouteScope.getBeanStore(VaadinRouteScope.java:412)
    at com.vaadin.flow.spring.scopes.AbstractScope.get(AbstractScope.java:44)
    at com.vaadin.flow.spring.scopes.VaadinRouteScope.get(VaadinRouteScope.java:67)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:371)
    ... 103 more

it seems to me this error is caused by this commit: https://github.com/mvysny/karibu-testing/commit/7d0435e03717aadf6d4e5bb8c9f26314a409ebca. I tested using version 1.3.16 and it works ok.

This is the code in my test class:

  private static final Routes routes = new Routes().autoDiscoverViews("the.package");

  @Autowired
  private ApplicationContext ctx;

  @BeforeEach
  public void mockVaadin() {
    final Function0<UI> uiFactory = UI::new;
    final SpringServlet servlet = new MockSpringServlet(routes, ctx, uiFactory);
    MockVaadin.setup(uiFactory, servlet);
  }

  @BeforeEach
  public void navigate() {
    UI.getCurrent()
        .navigate(AView.class);
    _assertOne(AView.class);
  }

and this is what I have in my POM:

    <dependency>
        <groupId>com.github.mvysny.kaributesting</groupId>
        <artifactId>karibu-testing-v10-spring</artifactId>
        <version>1.3.18</version>
        <scope>test</scope>
    </dependency>

The code that triggers the bug is something like:

@Route(...)
public class ARoute {
...
  @Inject
private TheClassThatCausesProblem the classe;
...
}

and:

@RouteScope
public class TheClassThatCausesProblem extends HorizontalLayout {
...
}
mvysny commented 1 year ago

Thank you for letting me know :+1: I'll try to add this test to Karibu itself, hopefully I'll be able to reproduce it on my machine. It's good to know that 1.3.16 worked okay. @brunovianarezende could you please check 1.3.17 whether it's also affected?

mvysny commented 1 year ago

Also, which version of vaadin-spring.jar are you using please, and which Vaadin version?

brunovianarezende commented 1 year ago

Thank you for letting me know +1 I'll try to add this test to Karibu itself, hopefully I'll be able to reproduce it on my machine. It's good to know that 1.3.16 worked okay. @brunovianarezende could you please check 1.3.17 whether it's also affected?

Yes, 1.3.17 is affected too.

brunovianarezende commented 1 year ago

Also, which version of vaadin-spring.jar are you using please, and which Vaadin version?

We use:

    <dependency>
      <groupId>com.vaadin</groupId>
      <artifactId>
        vaadin-spring-boot-starter
      </artifactId>
      <version>${vaadin.version}</version>
    </dependency>

where ${vaadin.version} is equal to 23.1.1, i.e. vaadin-spring is 23.1.1 and vaadin is 23.1.1.

mvysny commented 1 year ago

Sorry - I tried to upgrade Spring in Karibu-Testing in hopes to create a reproducible test, but Spring failed with random class incompatibility exceptions. However, let me try to figure out a blind workaround.

mvysny commented 1 year ago

The new implementation of retrieveExtendedClientDetails() seems to interfere with Spring. It was introduced during the fix of #118 . I'll add a flag to turn this functionality off as a workaround.

mvysny commented 1 year ago

The flag is called fakeExtendedClientDetails and it can be turned off starting with Karibu 1.3.20. Please see the documentation for more details. This is just a workaround though - a proper solution still needs to be found.

I'll therefore keep this ticket open. If someone feels brave enough to upgrade Spring in Karibu-Tests and write a test that reproduces this issue, please go ahead and create a PR. I'll merge the PR and proceed to fix the issue properly.