java - responseEntity 的问题。即使 ResponseEntity.HttpStatus 为 201,MockMvc.Perform() 也会给出 200 状态代码

标签 java junit controller mockito spring-boot-test

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

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:预期状态:<201>,但实际为:<200>

虽然我已经提到 RequestEntity 对象中的状态为 HttpStatus.CREATED,但为什么我得到的是 200。 如果有人可以帮忙请帮忙

最佳答案

尝试改变

public ResponseEntity<String> insertString(String str)

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

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

关于java - responseEntity 的问题。即使 ResponseEntity.HttpStatus 为 201,MockMvc.Perform() 也会给出 200 状态代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53051236/

相关文章:

java - 如何减少或增加方 block ,同时保持其绝对定位?

java - Hibernate 标准在 Web 应用程序中花费的时间太长,但在 SQLPlus 中却很快?

java - 为什么在 JUnitParams 方法中设置字段变量后它们为 null?

java - 如何正确退出字节码监视器?

java - 编写一个方法,该方法采用整数堆栈作为参数,并从堆栈底部开始切换连续的数字对

java - 在 Eclipse 中使用数据库集成进行用户界面 JUnit 测试

unit-testing - 如何从命令行运行 JUnit 测试用例

c# - 使用 AutoFac 的 MVC Controller 通用注入(inject)

spring - 如何将 detectorHandlerMethodsInAncestorContexts 设置为 true?

asp.net-mvc - 错误: Make sure that the controller has a parameterless public constructor webapi