mercredi 25 décembre 2019

Assertion error when getting paged cases with assembler

I have searched through so many questions on SO, but cannot find the answer that works for me.

The test:

@RunWith(SpringRunner.class)
@WebMvcTest(CalendarAPI.class)
public class CalendarAPITest {
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private CaseResourceAssembler caseResourceAssembler;

    @MockBean
    private CaseService caseService;

    @MockBean
    private UserCategoryService userCategoryService;

    @MockBean
    private UserService userService;

    @MockBean
    private CustomUserDetailsService customUserDetailsService;

    @Test
    public void getAllCasesForUser() throws Exception {
        User user = new User(1L, "Foo", "Bar", "foo@bar.com",
                "foobar1", "ROLE_STUDENT", new University());

        AppUserPrincipal principal = new AppUserPrincipal(user,"ROLE_STUDENT");

        given(this.userService.findById(1L)).willReturn(Optional.of(user));



        UserCategory testUserCategory = new UserCategory(1L, "test", user);

        given(this.userCategoryService.findById(1L)).willReturn(Optional.of(testUserCategory));

        PatientCase patientCase = new PatientCase(1L, "test", LocalDate.now(), "ward",
                "", "", "", "", "", "", "", "", "", "", "", false);

        given(this.caseService.findById(1L)).willReturn(Optional.of(patientCase));

        List<PatientCase> patientCaseList = new ArrayList<>();
        patientCaseList.add(patientCase);

        given(this.caseService.findAllByUserId(principal.getUser().getId())).willReturn(patientCaseList);

        Page<PatientCase> casePage = new PageImpl<PatientCase>(patientCaseList, PageRequest.of(PageModel.getPAGE(),
                PageModel.getSIZE(), Sort.by("case_date").descending()), patientCaseList.size());

        when(this.caseService.findAllCasesAsPagesForUser(eq(1L), any(PageRequest.class))).thenReturn(casePage);

        this.mockMvc = MockMvcBuilders
                .standaloneSetup()
                .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
                .build();

        mockMvc.perform(MockMvcRequestBuilders.get("/api/paged-cases")
                .with(user(principal))
                .contentType(MediaType.APPLICATION_JSON)
        )
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.patientCases", hasSize(5)))
                .andExpect(jsonPath("$.patientCases[*].caseTitle", hasItems()));
    }
}

And the calendar API is as follows:

@RepositoryRestController
@RequestMapping(path = "/api")
public class CalendarAPI {
    private CaseService caseService;
    private UserService userService;
    private PagedResourcesAssembler<PatientCase> pagedAssembler;
    private CaseResourceAssembler caseResourceAssembler;

    @Autowired
    public CalendarAPI(CaseService caseService,
                       UserService userService,
                       PagedResourcesAssembler<PatientCase> pagedAssembler,
                       CaseResourceAssembler caseResourceAssembler) {
        this.caseService = caseService;
        this.userService = userService;
        this.pagedAssembler = pagedAssembler;
        this.caseResourceAssembler = caseResourceAssembler;
    }

    @GetMapping(value = "/paged-cases")
    public ResponseEntity<?> getPagedCases(Pageable pageable, Authentication authentication) {
        // Get the currently logged in user.
        AppUserPrincipal principal = (AppUserPrincipal) authentication.getPrincipal();
        User user = userService.findById(principal.getUser().getId()).get();

        Page<PatientCase> casePage = caseService.findAllCasesAsPagesForUser(user.getId(), pageable);

        // if (pageable.getPageNumber() > casePage.getTotalPages()) {
        //     throw new CustomMissingResourceException("Page number exceeds total pages");
        // }

        return ResponseEntity.status(HttpStatus.OK)
                .body(pagedAssembler.toModel(casePage, caseResourceAssembler));
    }

}

But I keep getting the error that java.lang.AssertionError: Status Expected :200 Actual :404. Why is the error occuring because the path does exist and when I run it on my browser, it works. I think the issue may be to MockMvcRequestBuilders or RepositoryRestController with WebMvcTest. But I am not sure. I have looked all over, any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire