简体   繁体   English

SpringBoot MVC后响应实体返回null

[英]SpringBoot MVC Post Response Entity returning null

Response enitity should be returning a new ResponseEntity with a http status of OK/200. 响应enitity应该返回一个http状态为OK / 200的新ResponseEntity。 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. 但是,在我测试期间,它返回null,我可以看到它被设置为null但不明白为什么甚至如何。 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. 从图像中可以看出,create var为null,但是,Mockitio应该将其设置为createBlogPostResponse1,所以我不确定为什么设置null。

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. 正如JBnizet mockito在内部使用equals方法来匹配模拟方法调用的参数一样。 Try overriding equals method for BlogPost class. 尝试重写BlogPost类的equals方法。 If you do not want to override equals and you just want to match any method invocation on your mock - use any() matcher : 如果您不想覆盖equals并且只想匹配模拟上的任何方法调用 - 请使用any()匹配器:

    @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. 正如Mockito所说,依赖于equals()进行参数匹配,但是重写equals()以使单元测试成功通常不是一个好主意。 The equals() overriding has to have a sense for the concerned class. equals()重写必须对相关类有意义。 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 : 否则,这里有一个替代方案,你可以使用any()匹配器来检查param属性值的内容,例如:

 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. 虽然对于断言没有在对象断言上使用equals()我更喜欢AssertJ。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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