【问题标题】:Issue with responseEntity . MockMvc.Perform() gives 200 status code even if the ResponseEntity.HttpStatus is 201responseEntity 的问题。即使 ResponseEntity.HttpStatus 是 201,MockMvc.Perform() 也会给出 200 状态码
【发布时间】:2018-10-29 17:55:20
【问题描述】:

我试图模拟控制器的 post 方法。我希望它返回状态码 201(CREATED) 。但它给出了状态码 200。 下面给出的是代码 这是我的控制器类

package com.example.demo;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

public @RestController
class SomeController{

    @PostMapping("/insertString")
    public ResponseEntity<String> insertString(String str) {
        return new ResponseEntity<>(new String("monster"),HttpStatus.CREATED);
    }
}

这是我的junit测试套装

package com.example.demo;

import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.mockito.Mockito.anyString;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;   

@RunWith(SpringRunner.class)
@WebMvcTest(SomeController.class)
public class TestClass {

@Autowired
private MockMvc mockMvc;

@MockBean
private SomeController someController;

@Test
public void test() throws Exception {

    ResponseEntity<String> entity = new ResponseEntity<>(HttpStatus.CREATED);

    when(someController.insertString(anyString())).thenReturn(entity);

    RequestBuilder requestBuilder = MockMvcRequestBuilders
            .post("/insertString")
            .accept(MediaType.APPLICATION_JSON)
            .content("monkey")
            .contentType(MediaType.APPLICATION_JSON);

    MvcResult result = mockMvc.perform(requestBuilder)
            .andExpect(status().isCreated())
            .andReturn();
    System.out.println(result.getResponse().getContentAsString());
}

}

得到的错误是

java.lang.AssertionError:预期状态: 但原为:

虽然我在 RequestEntity 对象中提到了状态为 HttpStatus.CREATED,但为什么我得到 200。 如果有人可以帮助,请提供帮助

【问题讨论】:

  • 你弄明白了吗?

标签: java junit controller mockito spring-boot-test


【解决方案1】:

尝试改变

public ResponseEntity<String> insertString(String str)

public ResponseEntity<String> insertString(@RequestBody String str)

我已经用上面的代码运行了测试,它通过了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-20
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 2015-04-22
    • 2014-05-31
    • 2020-08-22
    相关资源
    最近更新 更多