dimanche 3 novembre 2019

Use TestEntityManager to REST API

I have this test which test my SponsorAPI...

@RunWith(SpringRunner.class)
@WebMvcTest(SponsorAPI.class)
public class SponsorAPITest {
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private SponsorService sponsorService;

    @MockBean
    private CharityService charityService;

    @Test
    public void shouldReturnTheTopFiveSponsors() throws Exception {
        Charity nspcc = new Charity(1L,
                "12345678",
                "National Society for the Prevention of Cruelty to Children",
                "Kids charity",
                "nspcc",
                "NSPCC",
                true);

        given(charityService.findById(1L)).willReturn(Optional.of(nspcc));

        Sponsor sponsor1 = new Sponsor(1L,
                "Foo Bar 1",
                nspcc,
                "Test action",
                "Test purpose",
                LocalDateTime.now(),
                LocalDateTime.now(),
                LocalDateTime.now().plusMonths(6),
                "foo-bar-1");

        Sponsor sponsor2 = new Sponsor(2L,
                "Foo Bar 2",
                nspcc,
                "Test action",
                "Test purpose",
                LocalDateTime.now(),
                LocalDateTime.now(),
                LocalDateTime.now().plusMonths(6),
                "foo-bar-2");

        Sponsor sponsor3 = new Sponsor(3L,
                "Foo Bar 3",
                nspcc,
                "Test action",
                "Test purpose",
                LocalDateTime.now(),
                LocalDateTime.now(),
                LocalDateTime.now().plusMonths(6),
                "foo-bar-3");

        Sponsor sponsor4 = new Sponsor(4L,
                "Foo Bar 4",
                nspcc,
                "Test action",
                "Test purpose",
                LocalDateTime.now(),
                LocalDateTime.now(),
                LocalDateTime.now().plusMonths(6),
                "foo-bar-4");

        Sponsor sponsor5 = new Sponsor(5L,
                "Foo Bar 5",
                nspcc,
                "Test action",
                "Test purpose",
                LocalDateTime.now(),
                LocalDateTime.now(),
                LocalDateTime.now().plusMonths(6),
                "foo-bar-5");

        Sponsor sponsor6 = new Sponsor(6L,
                "Foo Bar 5",
                nspcc,
                "Test action",
                "Test purpose",
                LocalDateTime.now(),
                LocalDateTime.now(),
                LocalDateTime.now().plusMonths(6),
                "foo-bar-6");

        SponsorProjection sProjection1 = new SponsorProjectionImpl(sponsor1.getFundraiserName(),
                sponsor1.getFurl(), 1L);
        SponsorProjection sProjection2 = new SponsorProjectionImpl(sponsor2.getFundraiserName(),
                sponsor2.getFurl(), 2L);
        SponsorProjection sProjection3 = new SponsorProjectionImpl(sponsor3.getFundraiserName(),
                sponsor3.getFurl(), 3L);
        SponsorProjection sProjection4 = new SponsorProjectionImpl(sponsor4.getFundraiserName(),
                sponsor4.getFurl(), 4L);
        SponsorProjection sProjection5 = new SponsorProjectionImpl(sponsor5.getFundraiserName(),
                sponsor5.getFurl(), 5L);
        SponsorProjection sProjection6 = new SponsorProjectionImpl(sponsor6.getFundraiserName(),
                sponsor5.getFurl(), 6L);

        List<SponsorProjection> sponsorProjectionList = List.of(sProjection1, sProjection2,
                sProjection3, sProjection4, sProjection5, sProjection6);
        testEntityManager.flush();
        testEntityManager.clear();
        given(sponsorService.findTopFiveSponsors(1L)).willReturn(sponsorProjectionList);

        mockMvc.perform(get("/api/findTopFiveSponsors/{charityId}", 1)
                .contentType(MediaType.APPLICATION_JSON))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(jsonPath("$", hasSize(5)))
                .andExpect(jsonPath("$[0].name", is("Foo Bar 1")))
                .andExpect(jsonPath("$[1].name", is("Foo Bar 2")))
                .andExpect(jsonPath("$[2].name", is("Foo Bar 3")))
                .andExpect(jsonPath("$[3].name", is("Foo Bar 4")))
                .andExpect(jsonPath("$[4].name", is("Foo Bar 5")));
    }
}

But the problem is that the top 5 sponsors should be displayed not 6. But with the above way .andExpect(jsonPath("$", hasSize(5))) line fails as it wants size 6.

To fix this issue, I am told to add the @DataJpaTest annotation and also autowire a TestEntityManager. Both of which I have done. But what am I supposed to do further? What changes are needed to the code?

Aucun commentaire:

Enregistrer un commentaire