如何解决如何在 Mockito 中模拟 Cutom 注释或任何注释
@GetMapping
@JsonView(View.IAm.class)
public User myProfile(@CurrentUser User user) throws ResourceNotFoundException {
if(user == null) throw new ResourceNotFoundException("Undefined User");
return userRepository.findById(user.getId()).orElseThrow(ResourceNotFoundException::new);
}
@CurrentUser 基本上是一个自定义注释,定义如下:
@Target({ElementType.ParaMETER,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@AuthenticationPrincipal
public @interface CurrentUser {}
这是我的控制器测试用例:
@test()
public void myProfile_return200() throws UnsupportedEncodingException,Exception {
User user = new User();
user.setId("1");
when(mockUserRepository.findById(eq("1"))).thenReturn(Optional.of(user));
mockedMvc.perform(get("/api/user").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andReturn().getResponse().getContentAsstring();
}
包括存储库和服务类在内的所有内容都被正确模拟,但我总是为@CurrentUser 获取空值。
知道我需要做什么来模拟自定义带注释的参数或任何带注释的参数。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。