Unit Test Spring MVC Rest Service: MockMVC, JUnit, Mockito

You may also like...

Subscribe
Notify of
guest
14 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Mahi
Mahi
November 17, 2016 23:08

Nice article. The code is working as expected as well. But the only issue I found is “UserControllerIntegrationTest” class is throwing exceptions. Is there any additional configuration needed to run the integration tests?

org.springframework.web.client.HttpClientErrorException: 404 Not Found

test_update_user_success(com.memorynotfound.test.UserControllerIntegrationTest) Time elapsed: 0.138 sec <<< ERROR!
org.springframework.web.client.HttpClientErrorException: 403 Forbidden

Vincent Cheng
Vincent Cheng
December 24, 2016 05:00

A nice tutorial, but I have a question, in my User class, I have a dateOfBirth field with reference type is LocalDate(from joda-time), what is the correct syntax for testing that field? I try “.andExpect(jsonPath(“$[0].dateOfBirth”, is(new LocalDate(1990, 12, 1))))”, but I am getting “Expected: is
but: was ”

Thank you for your help

Vincent Cheng
Vincent Cheng
December 25, 2016 06:09
Reply to  Vincent Cheng

OK, I found the solutions, I need to write the custom JsonSerializer class to Serialize the date I want

Memorynotfound.com
Memorynotfound.com
January 6, 2017 15:29
Reply to  Vincent Cheng

Nice to hear you’ve found a solution!

Yura
Yura
October 1, 2017 20:36

Hi, Thank you for the post and information. Have an issue. Trying to run unit tests with your reccomendations, but getting such error. java.lang.AssertionError: JSON path “$” Expected: a collection with size but: collection size was at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20) at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:74) at org.springframework.test.web.servlet.result.JsonPathResultMatchers$1.match(JsonPathResultMatchers.java:86) at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171) at com.softserve.edu.Resources.controller.LookUpControllerTest.testLoadResourceTypes(LookUpControllerTest.java:56) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) This is my TestClass public… Read more »

Memorynotfound.com
Memorynotfound.com
October 2, 2017 07:51
Reply to  Yura

Dear Yura,

You’ve created a resourceTypes list but have not inserted any objects into that list.
After you create the ResourceType instances, you have to insert them into the list you created.

Kr,
Memorynotfound

Yura
Yura
October 2, 2017 21:34

yeah, thanks, i find and recover this stupid mistake yesterday). but there is another error: Java.lang.AssertionError: JSON path “$[0].id” Expected: is 1 but: was 2 Such kind of mistake accures with this kind of controller method: @RequestMapping(value = “/lookUp/resourceTypes”, method = RequestMethod.GET) public List loadResourceTypes(){ return resourceTypeService.getInstances()); } BUT when i`m testing it with some DTO utill class and using static method: @RequestMapping(value = “/lookUp/resourceTypes”, method = RequestMethod.GET) public List loadResourceTypes(){ return DtoUtilMapper.resTypesToResTypesDTO(resourceTypeService.getInstances()); } public class DtoUtilMapper { public static List resTypesToResTypesDTO (List resTypes){ List resTypeDTOs = new ArrayList(); for (ResourceType resType : resTypes) { resTypeDTOs.add(new ResourceTypeDTO(resType.getId(), resType.getTableName(), resType.getTypeName())); }… Read more »

Memorynotfound.com
Memorynotfound.com
October 3, 2017 14:42
Reply to  Yura

Dear Yura,

What’s the difference between the first and second approach?
In the fist approach, you are directly returning the result,
But in the second approach you are mapping/copying the list with no other functionality?

Anyway, I cannot see your entire source-code. But from your stack-trace I can see it fails somewhere in the ‘ResourceTypeDTO’ on line 27. Do you have some kind of initialization going on in your constructor? I gan definitely see a NullPointerException occurring there..

Kr,
Memorynotfound

Laurent
Laurent
October 30, 2017 14:36

Hi,
Thank you for this tutorial.
Is it possible to use “spring-test” with JAX-WS Webservice ?

Mukul Varshney
Mukul Varshney
November 9, 2017 11:33

Is it possible to add Headers in the request??

Memorynotfound.com
Memorynotfound.com
November 9, 2017 12:31
Reply to  Mukul Varshney

Yes you can add headers using get(“/users).header(“X-Header”, “value”)

BRIJAN
BRIJAN
May 17, 2018 11:37

Nice tutorial

Dmitry
Dmitry
September 28, 2018 12:43

THANK U VERY MUCH! Best material. This is what I needed!

Abhijeet
Abhijeet
April 9, 2019 15:55

Thanks a lot for the blog it was funtastic example.

A very detailed way of Spring unit testing with mockito.

Saved me the precious time and efforts.

Thanks a lot .
Abhijeet

14
0
Would love your thoughts, please comment.x
()
x