Karibu-Testing offers basic support for Spring. Please see t-shirt shop example for an example on how to use Karibu-Testing with a Spring app.
Please see vaadin-spring-karibu-testing on an example for Spring+Karibu-Testing example project.
Add the following dependencies to your project's pom.xml
:
<dependencies>
<dependency>
<groupId>com.github.mvysny.kaributesting</groupId>
<artifactId>karibu-testing-v10-spring</artifactId>
<version>x.y.z</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.mvysny.kaributesting</groupId>
<artifactId>karibu-testing-v23</artifactId>
<version>x.y.z</version>
<scope>test</scope>
</dependency>
</dependencies>
The first dependency will add the Karibu-Testing core jar (karibu-testing-v10
) and a Spring integration
jar on top of that (karibu-testing-v10-spring
); the second dependency
will add additional support for testing Vaadin 23 components such as VirtualList
and MultiselectComboBox
.
Spring Security is not supported out-of-the-box: Spring Security usually uses Servlet Filter which requires Servlet Container to be up and running, yet Karibu-Testing doesn't start any Servlet Container. See Issue #47 for more details. One of the workarounds is to Manually Authenticate User with Spring Security, before navigating to a view.
There are ways to enable Spring Security in Karibu's faked environment though, please
take a look at issue #94 and
issue #180. Namely, users
reported the following snippet to work - must be called before MockVaadin.setup()
:
MockVaadin.mockRequestFactory = {
object : FakeRequest(it) {
override fun getUserPrincipal() = SecurityContextHolder.getContext().authentication
}
}
Java:
MockVaadin.INSTANCE.setMockRequestFactory(session -> new FakeRequest(session) {
@Override
public @Nullable Principal getUserPrincipal() {
return SecurityContextHolder.getContext().getAuthentication();
}
});
Note that this will only carry the currently logged-in user (Principal) from Spring Security over to the faked Vaadin environment - a job that's usually done by Spring servlet filter. In this faked environment the filter is not triggered, and therefore this manual step is necessary. Note that the code above doesn't log in the user: you'll need to log in user in Spring Security beforehand, either via annotations or manually. See below for an example on how to do that.
- The vaadin-spring-karibu-testing example app which demoes the Spring Security as well
- Browserless Testing of Vaadin Apps With Karibu Testing by Simon Martinelli.