- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试模拟 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/
现在我需要我的 Controller 根据业务逻辑返回 2 种不同类型的对象,所以我的问题是我应该使用 ResponseEntity 或者我应该使用: ResponseEntity 这种返回类型有
我知道原始类型在代码中很糟糕,而且 List和 List ,例如,是不同的东西。但是 ResponseEntity 的情况呢?和 ResponseEntity ?在 @RestController 中
在 Spring Webflux 中, ResponseEntity 与 Mono 作为休息 Controller 的返回类型有什么区别? 什么时候最合适? 跟进这个问题,假设我需要返回一个列表,或者
我正在重构一些代码,这是下面示例的更大范围。 “主”类中充满了这些 try/catch block ,并且模糊了代码正在执行的操作。 我没有使用 Spring,而是使用 JaxRs 来处理异常。这将是
我正在尝试模拟 Controller 的 post 方法。我希望它返回状态代码 201(CREATED) 。但它给出了状态代码 200。下面给出的是代码这是我的 Controller 类 packag
我经常在代码中使用的结构如下: @RestController public class HelloController { @Autowired private HelloService h
请找到下面提到的代码:我需要帮助为 ResponseEntity 编写 Mockito 条件: if(isObjectPresent(ePartnerRestRequestDTO)) {
这个问题已经有答案了: Why do I get the error "File cannot be resolved to a type"? (5 个回答) 已关闭 3 年前。 我尝试在我的其余端点
我有下面的函数,我试图用我的对象列表返回 ResponseEntity。我不知道我错过了什么。 @RequestMapping(method = RequestMethod.GET, path
我对 Spring 有点陌生。我做了以下方法: public ResponseEntity updateBorder(@Valid @RequestBody Borders borders) thro
我需要模拟一项服务。我在ResponseEntity中得到空值一边 mock 全类同学一边回答。 需要模拟的方法: public List getExpression(String expressVi
我想在浏览器中显示 Controller (使用 Spring)返回的 ResponseEntity 的主体: return new ResponseEntity<>(l.getReachableDa
我正在返回一个用于文件下载的 ResponseEntity。 @RequestMapping("/download") public ResponseEntity download(){ by
这个问题引用了问题: Setting the response content-type without using HttpServletResponse 使用以下代码: @RequestMappi
我正在开发一个 spring 3.2.7 应用程序,它通过输出字节数组 ResponseEntity 的 spring Controller 将存储在数据库中的签名作为 base64 字符串发送回用户
我对 Spring 框架还很陌生,我正在尝试通过开发示例项目来学习 也许这是一个愚蠢的问题,但我根本找不到任何帮助。 当验证规则未通过时,我的 Controller 正在使用转义字符串而不是对象的 j
如果使用@Cacheable 作为返回值'ResponseEntity',我会遇到序列化错误。 Caused by: org.springframework.data.redis.serializer
我使用 ResponseEntity 为 GET“api/v1/name”和 POST“api/v1/name”请求返回响应。 我的目标是不返回空值的响应,例如在 POST "api/v1/name"
我创建了一个通用异常 DTO,它也扩展了 RuntimeException。通过这种方式,可以在应用程序中使用它,也可以将其用作 DTO。问题是当我将 DTO 应用于 ResponseEntity 构
public void etisLogAround(ProceedingJoinPoint joinPoint, EtisLog etisLog) throws Throwable {
我是一名优秀的程序员,十分优秀!