简体   繁体   English

java.lang.AssertionError:状态 404

[英]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.

相关问题 @RequestMapping java.lang.AssertionError:预期状态:200 实际:404 - @RequestMapping java.lang.AssertionError: Status Expected :200 Actual :404 java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;404&gt; in Junit test - java.lang.AssertionError: Status expected:<200> but was:<404> in Junit test java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> java.lang.AssertionError:预期状态:&lt;200&gt;,但在静态服务中为:&lt;404&gt; - java.lang.AssertionError: Status expected:<200> but was:<404> in restful services MockMVC测试java.lang.AssertionError:预期状态:201实际:404 - MockMVC test java.lang.AssertionError: Status Expected :201 Actual :404 java.lang.AssertionError: Status expected:&lt;200&gt; but was:&lt;404&gt; with test case failed - java.lang.AssertionError: Status expected:<200> but was:<404> with test cases failing java.lang.AssertionError:预期状态:&lt;200&gt; 但原为:&lt;404&gt; Spring 引导页面未找到 - java.lang.AssertionError: Status expected:<200> but was:<404> Spring Boot Page Not found java.lang.AssertionError:预期状态:<200> 但为:<404> 对于 Spring 启动 Rest Controller - java.lang.AssertionError: Status expected:<200> but was:<404> for Spring Boot Rest Controller java.lang.AssertionError: 预期状态:&lt;200&gt; 但为:&lt;400&gt; - java.lang.AssertionError: Status expected:<200> but was:<400> java.lang.AssertionError:预期状态:&lt;200&gt;但原为:&lt;201&gt; - java.lang.AssertionError: Status expected:<200> but was:<201>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM