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

You may also like...

12
Leave a Reply

avatar
6 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
7 Comment authors
BRIJANMemorynotfound.comMukul VarshneyLaurentYura Recent comment authors
  Subscribe  
newest oldest
Notify of
Mahi
Guest
Mahi

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
Guest
Vincent Cheng

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
Guest
Vincent Cheng

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

Memorynotfound.com
Guest
Memorynotfound.com

Nice to hear you’ve found a solution!

Yura
Guest
Yura

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
Guest
Memorynotfound.com

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
Guest
Yura

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
Guest
Memorynotfound.com

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
Guest
Laurent

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

Mukul Varshney
Guest
Mukul Varshney

Is it possible to add Headers in the request??

Memorynotfound.com
Guest
Memorynotfound.com

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

BRIJAN
Guest
BRIJAN

Nice tutorial