Object MockVaadin
-
- All Implemented Interfaces:
public class MockVaadin
-
-
Field Summary
Fields Modifier and Type Field Description private StringuserAgentprivate Function1<MockHttpSession, MockRequest>mockRequestFactorypublic final static MockVaadinINSTANCE
-
Method Summary
Modifier and Type Method Description final static Unitsetup(Routes routes, Function0<UI> uiFactory, Set<Class<?>> lookupServices)Mocks Vaadin for the current test method:
The UI factory must provide a new, fresh instance of the UI, so that the tests start from a pre-known state.MockVaadin.setup(Routes().autoDiscoverViews("com.myapp"))final static Unitsetup(Routes routes, Function0<UI> uiFactory)Mocks Vaadin for the current test method:
The UI factory must provide a new, fresh instance of the UI, so that the tests start from a pre-known state.MockVaadin.setup(Routes().autoDiscoverViews("com.myapp"))final static Unitsetup(Routes routes)Mocks Vaadin for the current test method:
The UI factory must provide a new, fresh instance of the UI, so that the tests start from a pre-known state.MockVaadin.setup(Routes().autoDiscoverViews("com.myapp"))final static Unitsetup()Mocks Vaadin for the current test method:
The UI factory must provide a new, fresh instance of the UI, so that the tests start from a pre-known state.MockVaadin.setup(Routes().autoDiscoverViews("com.myapp"))final static Unitsetup(Function0<UI> uiFactory, VaadinServlet servlet, Set<Class<?>> lookupServices)Use this method when you need to provide a completely custom servlet (e.g. final UnitcloseCurrentUI(Boolean fireUIDetach)Properly closes the current UI and fire the detach event on it. final static UnittearDown()Cleans up and removes the Vaadin UI and Vaadin Session. final static UnitclientRoundtrip()Since UI Unit Testing runs in the same JVM as the server and there is no browser, the boundaries between the client and the server become unclear. final static UnitrunUIQueue(Boolean propagateExceptionToHandler, VaadinSession session)Runs all tasks scheduled by UI.access. final static UnitrunUIQueue(Boolean propagateExceptionToHandler)Runs all tasks scheduled by UI.access. final static UnitrunUIQueue()Runs all tasks scheduled by UI.access. final static UnitafterSessionClose(VaadinSession session, Function0<UI> uiFactory)Internal function, do not call directly. final StringgetUserAgent()Change & call setup to set a different browser. final UnitsetUserAgent(String userAgent)Change & call setup to set a different browser. final Function1<MockHttpSession, MockRequest>getMockRequestFactory()Creates MockRequest; override if you need to return a class that extends MockRequest and modifies its behavior. final UnitsetMockRequestFactory(Function1<MockHttpSession, MockRequest> mockRequestFactory)Creates MockRequest; override if you need to return a class that extends MockRequest and modifies its behavior. -
-
Method Detail
-
setup
@JvmOverloads() final static Unit setup(Routes routes, Function0<UI> uiFactory, Set<Class<?>> lookupServices)
Mocks Vaadin for the current test method:
MockVaadin.setup(Routes().autoDiscoverViews("com.myapp"))The UI factory must provide a new, fresh instance of the UI, so that the tests start from a pre-known state. If you're using Spring and you're getting UI from the injector, you must reconfigure Spring to use prototype scope, otherwise an old UI from the UI scope or Session Scope will be provided.
Sometimes you wish to provide a specific VaadinServletService, e.g. to override VaadinServletService.loadInstantiators and provide your own way of instantiating Views, e.g. via Spring or Guice. Please do that by extending MockVaadinServlet and overriding MockVaadinServlet.createServletService
createServletService(DeploymentConfiguration). Please consult MockService on what methods you must override in your custom service. Alternatively, seeMockSpringServleton how to extend your custom servlet and provide all necessary mocking code.- Parameters:
routes- all classes annotated with com.vaadin.flow.router.Route; use Routes.autoDiscoverViews to auto-discover all such classes.uiFactory- produces UI instances and sets them as current, by default simply instantiates MockedUI class.lookupServices- service classes to be provided to the lookup initializer
-
setup
@JvmOverloads() final static Unit setup(Routes routes, Function0<UI> uiFactory)
Mocks Vaadin for the current test method:
MockVaadin.setup(Routes().autoDiscoverViews("com.myapp"))The UI factory must provide a new, fresh instance of the UI, so that the tests start from a pre-known state. If you're using Spring and you're getting UI from the injector, you must reconfigure Spring to use prototype scope, otherwise an old UI from the UI scope or Session Scope will be provided.
Sometimes you wish to provide a specific VaadinServletService, e.g. to override VaadinServletService.loadInstantiators and provide your own way of instantiating Views, e.g. via Spring or Guice. Please do that by extending MockVaadinServlet and overriding MockVaadinServlet.createServletService
createServletService(DeploymentConfiguration). Please consult MockService on what methods you must override in your custom service. Alternatively, seeMockSpringServleton how to extend your custom servlet and provide all necessary mocking code.- Parameters:
routes- all classes annotated with com.vaadin.flow.router.Route; use Routes.autoDiscoverViews to auto-discover all such classes.uiFactory- produces UI instances and sets them as current, by default simply instantiates MockedUI class.
-
setup
@JvmOverloads() final static Unit setup(Routes routes)
Mocks Vaadin for the current test method:
MockVaadin.setup(Routes().autoDiscoverViews("com.myapp"))The UI factory must provide a new, fresh instance of the UI, so that the tests start from a pre-known state. If you're using Spring and you're getting UI from the injector, you must reconfigure Spring to use prototype scope, otherwise an old UI from the UI scope or Session Scope will be provided.
Sometimes you wish to provide a specific VaadinServletService, e.g. to override VaadinServletService.loadInstantiators and provide your own way of instantiating Views, e.g. via Spring or Guice. Please do that by extending MockVaadinServlet and overriding MockVaadinServlet.createServletService
createServletService(DeploymentConfiguration). Please consult MockService on what methods you must override in your custom service. Alternatively, seeMockSpringServleton how to extend your custom servlet and provide all necessary mocking code.- Parameters:
routes- all classes annotated with com.vaadin.flow.router.Route; use Routes.autoDiscoverViews to auto-discover all such classes.
-
setup
@JvmOverloads() final static Unit setup()
Mocks Vaadin for the current test method:
MockVaadin.setup(Routes().autoDiscoverViews("com.myapp"))The UI factory must provide a new, fresh instance of the UI, so that the tests start from a pre-known state. If you're using Spring and you're getting UI from the injector, you must reconfigure Spring to use prototype scope, otherwise an old UI from the UI scope or Session Scope will be provided.
Sometimes you wish to provide a specific VaadinServletService, e.g. to override VaadinServletService.loadInstantiators and provide your own way of instantiating Views, e.g. via Spring or Guice. Please do that by extending MockVaadinServlet and overriding MockVaadinServlet.createServletService
createServletService(DeploymentConfiguration). Please consult MockService on what methods you must override in your custom service. Alternatively, seeMockSpringServleton how to extend your custom servlet and provide all necessary mocking code.
-
setup
final static Unit setup(Function0<UI> uiFactory, VaadinServlet servlet, Set<Class<?>> lookupServices)
Use this method when you need to provide a completely custom servlet (e.g.
SpringServlet). Do not forget to create a specialized service which works in mocked environment. See below for details on how to do this.The UI factory must provide a new, fresh instance of the UI, so that the tests start from a pre-known state. If you're using Spring and you're getting UI from the injector, you must reconfigure Spring to use prototype scope, otherwise an old UI from the UI scope or Session Scope will be provided.
- Parameters:
uiFactory- produces UI instances and sets them as current, by default simply instantiates MockedUI class.servlet- allows you to provide your own implementation of VaadinServlet.lookupServices- service classes to be provided to the lookup initializer
-
closeCurrentUI
final Unit closeCurrentUI(Boolean fireUIDetach)
Properly closes the current UI and fire the detach event on it. Does nothing if there is no current UI.
-
tearDown
final static Unit tearDown()
Cleans up and removes the Vaadin UI and Vaadin Session. You can call this function in
afterEach{}block, to clean up after the test. This comes handy when you want to be extra-sure that the next test won't accidentally reuse old UI, should you forget to call setup properly.You don't have to call this function though; setup will overwrite any current UI/Session instances with a fresh ones.
-
clientRoundtrip
final static Unit clientRoundtrip()
Since UI Unit Testing runs in the same JVM as the server and there is no browser, the boundaries between the client and the server become unclear. When looking into sources of any test method, it's really hard to tell where exactly the server request ends, and where another request starts.
You can establish an explicit client boundary in your test, by explicitly calling this method. However, since that would be both laborous and error-prone, the default operation is that UI Unit Testing pretends as if there was a client-server roundtrip before every component lookup via the _get/_find/_expectNone/_expectOne call. See TestingLifecycleHook for more details.
Calls the following:
StateTree.runExecutionsBeforeClientResponse which runs all blocks scheduled via UI.beforeClientResponse
cleanupDialogs
If you'd like to test your ErrorHandler then take a look at runUIQueue instead.
-
runUIQueue
@JvmOverloads() final static Unit runUIQueue(Boolean propagateExceptionToHandler, VaadinSession session)
Runs all tasks scheduled by UI.access.
If VaadinSession.errorHandler is not set or propagateExceptionToHandler is false, any exceptions thrown from Commands scheduled via the UI.access will make this function fail. The exceptions will be wrapped in ExecutionException. Generally it's best to keep propagateExceptionToHandler set to false to make any exceptions fail the test; however if you're testing how your own custom VaadinSession.errorHandler responds to exceptions then set this parameter to true.
Called automatically by clientRoundtrip which is by default called automatically from TestingLifecycleHook. You generally don't need to call this method unless you need to test your ErrorHandler.
- Parameters:
propagateExceptionToHandler- defaults to false.
-
runUIQueue
@JvmOverloads() final static Unit runUIQueue(Boolean propagateExceptionToHandler)
Runs all tasks scheduled by UI.access.
If VaadinSession.errorHandler is not set or propagateExceptionToHandler is false, any exceptions thrown from Commands scheduled via the UI.access will make this function fail. The exceptions will be wrapped in ExecutionException. Generally it's best to keep propagateExceptionToHandler set to false to make any exceptions fail the test; however if you're testing how your own custom VaadinSession.errorHandler responds to exceptions then set this parameter to true.
Called automatically by clientRoundtrip which is by default called automatically from TestingLifecycleHook. You generally don't need to call this method unless you need to test your ErrorHandler.
- Parameters:
propagateExceptionToHandler- defaults to false.
-
runUIQueue
@JvmOverloads() final static Unit runUIQueue()
Runs all tasks scheduled by UI.access.
If VaadinSession.errorHandler is not set or propagateExceptionToHandler is false, any exceptions thrown from Commands scheduled via the UI.access will make this function fail. The exceptions will be wrapped in ExecutionException. Generally it's best to keep propagateExceptionToHandler set to false to make any exceptions fail the test; however if you're testing how your own custom VaadinSession.errorHandler responds to exceptions then set this parameter to true.
Called automatically by clientRoundtrip which is by default called automatically from TestingLifecycleHook. You generally don't need to call this method unless you need to test your ErrorHandler.
-
afterSessionClose
final static Unit afterSessionClose(VaadinSession session, Function0<UI> uiFactory)
Internal function, do not call directly.
Only usable when you are providing your own implementation of VaadinSession. See MockVaadinSession on how to call this properly.
-
getUserAgent
final String getUserAgent()
Change & call setup to set a different browser.
The default is Firefox 94 on Ubuntu Linux.
-
setUserAgent
final Unit setUserAgent(String userAgent)
Change & call setup to set a different browser.
The default is Firefox 94 on Ubuntu Linux.
-
getMockRequestFactory
final Function1<MockHttpSession, MockRequest> getMockRequestFactory()
Creates MockRequest; override if you need to return a class that extends MockRequest and modifies its behavior.
-
setMockRequestFactory
final Unit setMockRequestFactory(Function1<MockHttpSession, MockRequest> mockRequestFactory)
Creates MockRequest; override if you need to return a class that extends MockRequest and modifies its behavior.
-
-
-
-