【问题标题】:Unit testing for POST request using Mockido error使用 Mockido 错误对 POST 请求进行单元测试
【发布时间】:2017-11-19 17:35:53
【问题描述】:

测试时我期望的状态是 200,但我现在却得到 404。

我对 Mockido 还很陌生,所以如果我缺少一些简单的东西。请告诉我。

我在我的控制器中创建了一个 POST 请求,它接受一个 Long 对象列表。如果没有发生异常,则返回 OK 状态:

@PostMapping(path = "/postlist")
public ResponseEntity<Void> updateAllInList(@RequestBody List<Long> ids) {
    try {
        // method from ControllerService.java here using ids 
        return ResponseEntity.status(HttpStatus.OK).body(null);
    } catch (InvalidContentException e) {
        return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(null);
    }

当我使用 REST 客户端 POST 时,我得到了正确的结果。我发布的原始有效负载是这样的:

[
     2, 1
]

但是,单元测试给了我一个 404。

我创建Test类的方式是这样的:

    @RunWith(SpringJUnit4ClassRunner.class)
    @WebAppConfiguration
    @ContextHierarchy({ @ContextConfiguration(classes = RootConfiguration.class), @ContextConfiguration(classes = WebConfiguration.class) })
    @Category(UnitTest.class)
    public class ControllerTest {

         private static final String POST_REQUEST = "[ 2, 1 ]";

         @Autowired private WebApplicationContext webApplicationContext;
         @Autowired private ControllerService controllerService;

         private MockMvc mockMvc;


         @Before
         public void setUp() throws Exception {

              this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();

              doNothing().when(this.controllerService).updateAllInList(anyList());
             doThrow(InvalidContentException.class).when(this.controllerService).updateAllInList(null);
        }

        @Test
        public void updateList() throws Exception {
            this.mockMvc.perform(post("http://testhost/api/configuration/postlist").contentType(MediaType.APPLICATION_JSON_UTF8).content(POST_REQUEST))
                    .andExpect(status().isOk());
        }


        @Configuration
        static class RootConfiguration {


            @Bean
            public ControllerService ControllerService() {
                return Mockito.mock(ControllerService.class);
            }
        }


        @Configuration
        @EnableWebMvc
        static class WebConfiguration extends WebMvcConfigurerAdapter {

             @Autowired
             private ControllerService controllerService;


             @Bean
             public Controller controller() {
                  return new Controller(controllerService);
             }
         }
    }

我的理论是在我的测试课中我插入了错误的内容。但是为什么我们不能插入与我们从真实 POST 原始负载中使用的内容相同的内容呢?

谢谢。

【问题讨论】:

  • POST_REQUEST 字符串在我看来不像 JSON?
  • @KarlNicholas 嗨,如果我在 POST 时的原始有效负载以这种格式工作,我是否遗漏了什么导致测试以这种格式失败?我猜一些额外的字符会在发布时自动添加到原始有效负载中,但它们是什么?谢谢。
  • 404 未找到:'testhost/api/configuration/postlist` 是真实的 URL 吗?

标签: java spring unit-testing jpa mockito


【解决方案1】:

当您使用MockMvc 时,您希望触发控制器的映射,而不是 HTTP 服务器。

而不是mockMvc.perform(post("http://testhost/api/configuration/postlist")...

试试mockMvc.perform(post("/configuration/postlist")...

【讨论】:

  • 感谢您的回答。但是,即使在尝试了我的映射的所有组合之后,我仍然得到 404。你认为我的内容和我的给定(在设置方法中)是否正确?
猜你喜欢
  • 1970-01-01
  • 2014-03-15
  • 1970-01-01
  • 2020-09-24
  • 2018-03-13
  • 2017-06-05
  • 2023-03-20
  • 1970-01-01
相关资源
最近更新 更多