Combining Jersey, Spring And Mockito To Create Web MVC Test Using Mock Beans — Web Resource Slice Testing
Combining Jersey, Spring And Mockito To Create Web MVC Test — Web Resource Slice Testing

Member-only story

JerseyTest With Spring And Mockito — Mimics Spring Slice Testing With @WebMvcTest And @MockBean

Amith Kumar

--

In a RESTFul java application built with Jersey JAX-RS implementation and Spring, the ability to test API endpoints without loading the entire application context is not an out of the box solution.

Even with the vast coverage of Spring Test Auto-configuration Annotations , none of these covers the slice testing of REST resources built with JAX-RS specifications.

As well within Jersey framework with extended support for Spring, built in JerseyTest, will auto detect spring presence in the classpath and will seek to load entire application context if not already loaded.

In this blog, we will learn to mimic Spring @WebMvcTest & @MockBean slice testing behaviour using JerseyTest and Mockito .

When running resource test using JerseyTest , it eventually invokes createSpringContext method of class SpringComponentProvider of library jersey-spring.

createSpringContext method code of SpringComponentProvider class
SpringComponentProvider#createSpringContext Code

Here it first checks the property contextConfig to see if the application context is already loaded, otherwise it loads the context from the XML file by path property…

--

--

No responses yet