I'm trying to test a Vaadin view. One of the Component
s is a div containing an ApexCharts
.
While I'm testing another component of the page (some Grid) I'm getting the following error:
java.lang.NullPointerException: Cannot invoke "com.vaadin.flow.server.VaadinService.getDeploymentConfiguration()" because the return value of "com.vaadin.flow.server.VaadinService.getCurrent()" is null
For the line ApexChartsBuilder.get()...build()
Why?
This is as expected as when view is instantiated in isolation, the Framework's internal classes such as UI, VaadinSession, VaadinService etc. are not automatically being instantiated. That will mean that VaadinService.getCurrent(), UI.getCurrent() will return null, as the thread locals has not been set.
If you wish to do unit testing on your views, you need to have Vaadin's internal classes mocked. For simple cases it is not super hard to do, but in more complex cases can be a deep rabit hole.
You can find a simple component UI unit test with basic Vaadin mocks here:
https://github.com/TatuLund/bean-table/blob/nextlts/src/test/java/org/vaadin/tatu/BeanTableTest.java
In Vaadin 23, the commercial TestBench add-on comes with UI Unit Test library, which is essentially containing these mocks. With Vaadin 14 you could try to use Karibu, which is essentially the same.
You can find example of UI Unit Test of a view here
https://github.com/TatuLund/TwinColSelect/blob/nextlts/src/test/java/org/vaadin/tatu/ViewTest.java