[英]java.lang.AssertionError: Status 404
Im having 404 error on this test, Im trying to figure out why its giving me this error could someone help me please我在这个测试中有 404 错误,我试图弄清楚为什么它给我这个错误可以请有人帮助我
this is my controller:这是我的 controller:
@PostMapping("/infoUtile/add/{idUser}")
public InformationUtile addOrEditProcedure(@PathVariable(value = "idUser") Long id){
User user = this.userService.getUser(id);
InformationUtile info = new InformationUtile();
info.setUser(user);
return this.infoUtileService.addOrEditInfoUtile(info);
}
and this is the test that I wrote这是我写的测试
@Autowired
private MockMvc mockMvc;
@MockBean
InformationUtileService informationUtileService;
@MockBean
UserService userService;
ObjectMapper mapper = new ObjectMapper();
@Test
public void givenInformationsUtiles_whenGetInfoUtiles_thenReturnJsonArray()
throws Exception {
InformationUtile informationUtile = new InformationUtile();
informationUtile.setId(1);
informationUtile.setContent("oumaima");
informationUtile.setDescription("test");
Media medias = new Media();
medias.setType("image/png");
medias.setUrl("C:\\files\\actualite\\32769\\adobexd.png");
List<Media> allMedias = new ArrayList<Media>();
allMedias.add(medias);
informationUtile.setMedias(allMedias);
OngoingStubbing<User> user = Mockito.when(userService.getUser(Mockito.anyLong())).thenReturn(new User());
Mockito.when(informationUtileService.addOrEditInfoUtile(Mockito.any(InformationUtile.class))).thenReturn(informationUtile);
mockMvc.perform(post("/infoUtile/add/{id}",informationUtile.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(mapper.writeValueAsBytes(informationUtile)))
.andExpect(status().isOk());
}
log日志
I had the same issue and in my case the solution was to use full path我有同样的问题,在我的情况下,解决方案是使用完整路径
from @RequestMapping
(controller annotation) + @PostMapping
(method annotation)来自
@RequestMapping
(控制器注解)+ @PostMapping
(方法注解)
I don't know if this is the case for you.我不知道你是否属于这种情况。 You don't show your controller class.
您没有显示您的 controller class。 But if you have something like this:
但如果你有这样的事情:
@RestController
@RequestMapping("/api/v1")
public class SomeController {
@PostMapping("/infoUtile/add/{idUser}")
public InformationUtile addOrEditProcedure(@PathVariable(value = "idUser") Long id) {...}
}
then the path in test will be那么测试中的路径将是
mockMvc.perform(post("/api/v1/infoUtile/add/{idUser}",informationUtile.getId())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.