gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:04:58 28 4
gpt4 key购买 nike

我正在尝试模拟 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com