3

包括 spring-boot-starter-data-rest 和另一个持久性存储库(在我的情况下是 spring-boot-starter-data-mongodb),您在 REST 中自动拥有 CRUD 的端点(假设您有一个包含 main 方法的主类,用 @SpringBootApplication 注释在正确的级别上)为您创建。例如,当您访问http://locahost:8080/api/sampleEntities时,您会在 basePath 下获得一个带有 GET、PUT、POST、DELETE 等的端点,因为我有一个 SampleRepository 来管理 SampleEntity 实例。

如何在 JUnit 5 + Mockito + Spring Boot 中测试这些端点?

这是我当前的测试用例供参考,但我能看到的唯一端点是配置文件端点。

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());
    }
}

运行实际的应用程序并指向http://localhost:8080/api/sampleEntities工作正常。

实际的应用程序类如下所示:

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);
    }

}

有人可以给我一些关于如何构建我的测试类和方法以测试由 spring-boot-starter-data-rest 自动生成的休息端点的代码示例吗?

4

0 回答 0