包括spring-boot-starter-data-rest和另一个持久性存储库(在我的例子中是spring-boot-starter-data-mongodb),您会在REST中自动创建CRUD的终结点(假设您有一个包含注释了main方法的主类)为您创建@SpringBootApplication的正确级别)。例如,由于我有一个SampleRepository来管理SampleEntity实例,因此在访问http://locahost:8080/api/sampleEntities时,可以在basePath下获得具有GET,PUT,POST,DELETE等终结点的端点。
如何在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自动生成的其余端点吗?