3

Including spring-boot-starter-data-rest and another persistence repository (in my case spring-boot-starter-data-mongodb) you have endpoints for CRUD in REST automatically (assuming you have a main class containing main method annotated with @SpringBootApplication on the correct level) created for you. For example you get an endpoint with GET, PUT, POST, DELETE etc under your basePath when visiting http://locahost:8080/api/sampleEntities since I have a SampleRepository to manage SampleEntity instances.

How can I test these endpoints in JUnit 5 + Mockito + Spring Boot?

This is my current test case for reference, but the only endpoint I can see is the profile endpoint.

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.context.ApplicationContext;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import java.util.Collections;
import java.util.List;

import static java.util.Arrays.asList;
import static org.mockito.Mockito.doReturn;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class SampleEntityRestTest {

    @LocalServerPort private int port;
    @MockBean private SampleRepository sampleRepository;

    @Autowired private ApplicationContext applicationContext;
    @Autowired private TestRestTemplate testRestTemplate;

    @DisplayName("Mocking sample repository works.")
    @Test
    public void checkMockingWorks() {
        List<SampleEntity> sampleEntityList = asList(SampleEntity.builder().firstName("firstname").lastName("lastname").email("some@some.com").build());
        Page<SampleEntity> page = new PageImpl<>(sampleEntityList);
        doReturn(page).when(sampleRepository).findAll(Pageable.unpaged());

        SampleRepository sampleRepositoryFromContext = applicationContext.getBean("sampleRepository", SampleRepository.class);
        List<SampleEntity> sampleEntityListFromContext = sampleRepositoryFromContext.findAll(Pageable.unpaged()).getContent();
        Assertions.assertFalse(sampleEntityListFromContext.isEmpty());
        Assertions.assertEquals("firstname", sampleEntityListFromContext.get(0).getFirstName());
        Assertions.assertEquals("lastname", sampleEntityListFromContext.get(0).getLastName());
        Assertions.assertEquals("some@some.com", sampleEntityListFromContext.get(0).getEmail());
    }

    @Test
    public void testCrudOfSampleEntity() {
        List<SampleEntity> sampleEntityList = asList(SampleEntity.builder().firstName("firstname").lastName("lastname").email("some@some.com").build());
        Page<SampleEntity> page = new PageImpl<>(sampleEntityList);
        doReturn(page).when(sampleRepository).findAll(Pageable.unpaged());

        ResponseEntity<Resources<SampleEntity>> resourcesResponseEntity = testRestTemplate.exchange(
                Constants.ENDPOINT + ":" + port + "/api/" + Constants.SAMPLE_ENTITY,
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<Resources<SampleEntity>>() {},
                Collections.emptyList());
        Assertions.assertEquals(200, resourcesResponseEntity.getStatusCode());
    }
}

Running the actual application and pointing to http://localhost:8080/api/sampleEntities works fine.

The actual application class looks like this:

package com.somepackage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SomeRestApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(SomeRestApiApplication.class, args);
    }

}

Can someone give me some code example on how to build my test classes and methods in order to test rest endpoints generated automatically by the spring-boot-starter-data-rest?

vbledar
  • 51
  • 2

0 Answers0