Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Spring's MockMvc with Jersey resources?

I have successfully integrated Spring and Jersey via Spring Boot' starter POMs, and I have a couple Jersey endpoints. Now I'd like to unit test the resources. I can't seem to get MockMvc working. I get a 404 error when attempting to GET a resource endpoint.

I know there is a Jersey test framework out there, but it appears to launch a server. I'm hoping to avoid "integration" type tests and keep this as simple as possible. Can I do this with MockMvc?

like image 667
John O'Conner Avatar asked Jan 06 '16 23:01

John O'Conner


People also ask

What is the use of MockMvc?

MockMvc provides support for Spring MVC testing. It encapsulates all web application beans and makes them available for testing. We'll initialize the mockMvc object in the @BeforeEach annotated method, so that we don't have to initialize it inside every test.

Is MockMvc integration test?

Technically speaking, tests using MockMvc are in the boundaries between unit and integration tests. They aren't unit tests because endpoints are tested in integration with a Mocked MVC container with mocked inputs and dependencies.

Is MockMvc thread safe?

From a technical point of view MockMvc is not thread-safe and shouldn't be reused. These setters are package private and a MockMvc instance can be acquired only through MockMvcBuilders . Hence you can't manipulte a MockMvc instance afterwards so that it is actually resuable across multiple tests.

What is MockMvc content?

MockMVC class is part of Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container. In this MockMVC tutorial, we will use it along with Spring boot's WebMvcTest class to execute Junit testcases which tests REST controller methods written for Spring boot 2 hateoas example.


1 Answers

Unfortunately doesn't seem to be possible as MockMvc doesn't actually start servlet container.

You can use

  1. RestTemplate to fire requests against server started on localhost. Here are examples from Spring Boot repo: Example 1 and Example 2.
  2. Rest Assured library
like image 74
luboskrnac Avatar answered Oct 05 '22 10:10

luboskrnac