gpt4 book ai didi

java - Springboot测试如何将controller的参数解析为对象

转载 作者:行者123 更新时间:2023-11-30 05:40:29 27 4
gpt4 key购买 nike

我在 Controller 中有一个像这样的方法签名。当我尝试为其编写单元测试时。它返回 500 而不是 404。看起来它无法将 {id} 转换为可选我需要做任何设置才能将参数自动转换为对象吗?

谢谢

@RequestMapping("/propagationStores")
public class PropagationStoreController {


private StoreRepository storeRepository;
private CustomValidator validator;
public PropagationStoreController(StoreRepository storeRepository) {
this.storeRepository = storeRepository;

}

@GetMapping(value = "/{id}")
public Resource<StoreDto> getById(@PathVariable("id") Optional<Store> storeOptional) {

return storeOptional
.map(StoreConverter::toDto)
.map(store -> {
Resource<StoreDto> resource = new Resource<>(store);
resource.add(new Link("http://localhost").withTitle("localhost"));
return resource;
}).orElseThrow(ResourceNotFoundException::new);
}

当我尝试使用以下代码测试 getById 方法时。我得到了 500 而不是 400

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class PropagationStoreControllerIT {


@MockBean
StoreRepository storeRepository;
@MockBean
CustomValidator customValidator;


@Autowired
private MockMvc mockMvc;

@Test
public void testGetById() throws Exception {
when(storeRepository.findById(1l)).thenReturn(Optional.empty());
mockMvc.perform(get("/propagationStores/1")).andDo(print()).andExpect(status().is4xxClientError());
}
}

我原本期待状态 404,但我得到的是 500。

日志如下。

   MockHttpServletRequest:
HTTP Method = GET
Request URI = /propagationStores/1
Parameters = {}
Headers = []
Body = null
Session Attrs = {}

Handler:
Type = local.tux.propagation.controller.PropagationStoreController
Method = public org.springframework.hateoas.Resource<local.tux.propagation.dto.Store$StoreDto> local.tux.propagation.controller.PropagationStoreController.getById(java.util.Optional<local.tux.propagation.evaluator.domain.Store>)

Async:
Async started = false
Async result = null

Resolved Exception:
Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException

ModelAndView:
View name = null
View = null
Model = null

FlashMap:
Attributes = null

MockHttpServletResponse:
Status = 500
Error message = null
Headers = []
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []

MockHttpServletRequest:
HTTP Method = GET
Request URI = /propagationStores/1
Parameters = {}
Headers = []
Body = null
Session Attrs = {}

Handler:
Type = local.tux.propagation.controller.PropagationStoreController
Method = public org.springframework.hateoas.Resource<local.tux.propagation.dto.Store$StoreDto> local.tux.propagation.controller.PropagationStoreController.getById(java.util.Optional<local.tux.propagation.evaluator.domain.Store>)

Async:
Async started = false
Async result = null

Resolved Exception:
Type = org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException

ModelAndView:
View name = null
View = null
Model = null

FlashMap:
Attributes = null

MockHttpServletResponse:
Status = 500
Error message = null
Headers = []
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []

java.lang.AssertionError: Range for response status value 500
Expected :CLIENT_ERROR
Actual :SERVER_ERROR

最佳答案

将 Controller 方法定义为:

public Resource<StoreDto> getById(@PathVariable("id") Optional<String> id) {
......
}

id 可以转换为字符串或数字,但不能转换为 Store 类。

关于java - Springboot测试如何将controller的参数解析为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55751235/

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