cuốn sách gpt4 ai đã làm

java - vấn đề với phản hồiEntity. MockMvc.Perform() cung cấp 200 mã trạng thái mặc dù ResponseEntity.HttpStatus là 201

In lại Tác giả: Vũ trụ không gian Thời gian cập nhật: 2023-11-04 10:04:58 28 4
mua khóa gpt4 Nike

Tôi đang cố gắng chế nhạo phương thức đăng bài của Người điều khiển. Tôi muốn nó trả về mã trạng thái 201 (TẠO). Nhưng nó cho mã trạng thái 200. Đưa ra dưới đây là mã đây là lớp Trình điều khiển của tôi

gói com.example.demo;

nhập org.springframework.http.HttpStatus;
nhập org.springframework.http.ResponseEntity;
nhập org.springframework.web.bind.annotation.PostMapping;
nhập org.springframework.web.bind.annotation.RestController;

công khai @RestController
lớp SomeController{

@PostMapping("/insertString")
công khai ResponseEntity InsertString(String str) {
trả về ResponseEntity mới<>(new String("monster"),HttpStatus.CREATED);
}
}

Đây là máy chủ thử nghiệm Junit của tôi

gói com.example.demo;

nhập tĩnh org.junit.Assert.*;
nhập org.mockito.Mockito.when tĩnh;
nhập tĩnh org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
nhập org.mockito.Mockito.anyString tĩnh;

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

@RunWith(SpringRunner.class)
@WebMvcTest(SomeController.class)
lớp công khai TestClass {

@Autowired
private MockMvc mockMvc;

@MockBean
riêng tư SomeController someController;

@Bài kiểm tra
public void test() ném ngoại lệ {

ResponseEntity thực thể = ResponseEntity mới<>(HttpStatus.CREATED);

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

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

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

}

Nhận sai là

java.lang.AssertionError: Trạng thái dự kiến: <201>, nhưng thực tế: <200>

Mặc dù tôi đã đề cập rằng trạng thái trong đối tượng requestEntity là HttpStatus.CREATED, nhưng tại sao tôi lại nhận được 200. Nếu ai có thể giúp đỡ xin vui lòng giúp đỡ

câu trả lời hay nhất

cố gắng thay đổi

public ResponseEntity InsertString(String str)

phản hồi công khai InsertString(@RequestBody String str)

Tôi đã chạy thử nghiệm với đoạn mã trên và nó đã thành công.

Liên quan đến câu hỏi của java-responseEntity. MockMvc.Perform() sẽ cung cấp 200 mã trạng thái mặc dù ResponseEntity.HttpStatus là 201, chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/53051236/

28 4 0
không gian vũ trụ
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress