简体   繁体   中英

SpringBoot MVC Post Response Entity returning null

Response enitity should be returning a new ResponseEntity with a http status of OK/200. However, during my testing it is returning as null i can see where it is being being set to null but dont understand why or even how. I am sure it's a simple thing i've missed but just cant see it.

As can be seen in the images, the create var is null, however, Mockitio should be setting this to createBlogPostResponse1, so i am not sure why null is being set.

Vars / services实例化 create被设置为null

Thanks for any information and help on this.

Test

public static ResponseEntity createBlogPostResponse1 = new ResponseEntity(HttpStatus.OK);


@Test
public void createNewBlogPost() throws Exception {
    String url = TestHelper.URL + "/blogPost/createNewBlogPost";
    when(postService.createNewBlogPost(blogPost1)).thenReturn(TestHelper.createBlogPostResponse1);
    mockMvc.perform(post(url)
            .contentType(MediaType.APPLICATION_JSON_UTF8)
            .content(TestHelper.asJsonString(blogPost1)))
            .andExpect(status().isOk())
            .andReturn();

    verify(postService, times(1)).createNewBlogPost(blogPost1);
    verifyNoMoreInteractions(postService);
}

Controller

ResponseEntity create = postService.createNewBlogPost(cleanBlogPost);

Service

@Override
public ResponseEntity createNewBlogPost(BlogPost createNewBlogPost) {
    return new ResponseEntity(HttpStatus.OK);
}

As pointed by JBnizet mockito uses equals method internally to match arguments on your mock method invocation. Try overriding equals method for BlogPost class. If you do not want to override equals and you just want to match any method invocation on your mock - use any() matcher :

    @Test
    public void createNewBlogPost() throws Exception {
        String url = TestHelper.URL + "/blogPost/createNewBlogPost";
        when(postService.createNewBlogPost(Mockito.any(BlogPost.class))).thenReturn(TestHelper.createBlogPostResponse1);
        mockMvc.perform(post(url)
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .content(TestHelper.asJsonString(blogPost1)))
                .andExpect(status().isOk())
                .andReturn();

        verify(postService, times(1)).createNewBlogPost(Mockito.any(BlogPost.class));
        verifyNoMoreInteractions(postService);
    }

If you want to gain some basic konwledge about matchers try this tutorial.

As said Mockito relies on equals() for argument matching but overriding equals() to make a unit test successful is generally not a good idea. The equals() overriding has to have a sense for the concerned class. If the overriding makes sense, go for it. Otherwise here is an alternative where you could use the any() matcher combined to a check on the content of the param properties values such as :

 when(postService.createNewBlogPost(Mockito.any(BlogPost.class)))
.then( answer -> { 
                   BlogPost post = (BlogPost) invocation.getArguments()[0]; // get the first arg
                   Assert.assertEquals(blogPost1.getFoo(), post.getFoo());
                   Assert.assertEquals(blogPost1.getBar(), post.getBar());
                   return TestHelper.createBlogPostResponse1();
                   });

While for assertions without using equals() on the object to assert I would prefer AssertJ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM