Member-only story
JerseyTest With Spring And Mockito — Mimics Spring Slice Testing With @WebMvcTest And @MockBean
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
.
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…