karibu-testing-v23
Karibu Testing, support for browserless Vaadin testing in Kotlin
The Unit Testing library for Vaadin.
When building apps with Vaadin Flow, your app manipulates Vaadin components server-side, while the browser is just a mere "reflection" of the server-side state (The "reflection" part - state transfer from server to client - is handled by Vaadin Flow). To test your app, you want your tests to focus on the server-side where your logic resides, while skipping the "unimportant" browser part "distraction".
Karibu-Testing gives you the ability to call UI.getCurrent()
straight from your JUnit
test methods, and receive a
meaningful result in the process. You can call UI.navigate()
to navigate around in your app;
you can instantiate your components and views directly from your JUnit test methods.
In order to do so, Karibu-Testing mocks CurrentRequest
, VaadinSession
and others in your
currently running JVM (in which your JUnit tests run).
Karibu-Testing only runs in JVM and only tests server-side code. There is no browser running
and no JavaScript code running, which means that there is no communication between the browser and the server,
which means that you don't even need to run the servlet container. You simply create new instance
of your Vaadin form, modify the field values directly, then simulate a click to the "Save" button
and see the binder validations running. You can even call UI.navigate()
to
navigate to a view; the navigation is handled completely server-side and adds components to UI.getCurrent()
.
You then assert on the contents of UI.getCurrent()
to verify that your code ran properly.
In short, Karibu-Testing is here to perform:
- containerless testing: You don't need to launch the servlet container. Karibu-Testing creates Vaadin Session, the UI and other necessary Vaadin classes straight in the JVM which runs your JUnit tests.
- browserless testing: You look up components straight from
UI.getCurrent()
, bypassing browser and the JavaScript->Server bridge completely. You directly call methods on your server-side View classes and on the server-side Java Vaadin components.
Check out a 30 second video of live coding with Karibu-Testing to get a taste on how simple this library is.
The library supports Kotlin, Java and Groovy (Groovy support for Vaadin 14+ only).
Sample code
import static com.github.mvysny.kaributesting.v10.LocatorJ.*; public class MyUITest { private static Routes routes; @BeforeAll public static void createRoutes() { routes = new Routes().autoDiscoverViews("com.vaadin.flow.demo"); } @BeforeEach public void setupVaadin() { MockVaadin.setup(routes); } @AfterEach public void tearDownVaadin() { MockVaadin.tearDown(); } @Test public void testGreeting() { // simulate a button click as if clicked by the user _click(_get(Button.class, spec -> spec.withCaption("Click me"))); // look up the Example Template and assert on its value assertEquals("Clicked!", _get(ExampleTemplate.class).getValue()); } }
Links
Compatibility
Was this helpful? Need more help?
Leave a comment or a question below. You can also join
the chat on Discord or
ask questions on StackOverflow.
Version
- Released
- 2024-08-01
- Maturity
- STABLE
- License
- Apache License 2.0
Compatibility
- Framework
- Vaadin 14+
- Vaadin 24+ in 2.0.1
- Browser
- N/A