Refer to:
https://www.baeldung.com/spring-boot-testing
a. JUnit test 的类型以及如何取舍:
Unit tests that can run in isolation as well as integration tests that will bootstrap Spring context before executing tests. 两种,isolation, integration test. Ideally, we should keep the integration tests separated from the unit tests and should not run along with the unit tests.The integration tests need to start up a container to execute the test cases.
b. 关于Integration Test
The @SpringBootTest annotation can be used when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests.
@RunWith(SpringRunner.class) is used to provide a bridge between Spring Boot test features and JUnit,Whenever we are using any Spring Boot testing features in our JUnit tests, this annotation will be required.
1.Test Dao(reposotory层) : DataJpaTest 可以自动生成内存数据库,下面这个EntityManager是用的TestEntityManager, 可以很方便的把数据先插入到数据库中。JPA可以方便的自动生成 H2结构,并方便的插入数据,那么mybatis 怎么办呢?
@Test public void whenFindByName_thenReturnEmployee() { // given Employee alex = new Employee("alex"); entityManager.persist(alex); entityManager.flush(); // when Employee found = employeeRepository.findByName(alex.getName()); // then assertThat(found.getName()) .isEqualTo(alex.getName()); }
2. Test service层
Ideally, we should be able to write and test our Service layer code without wiring in our full persistence layer.
2.1 使用 @TestConfiguration 重新生成Service(只在Test环境中有效),重新生成的Service中Mock repository 层
2.2 @MockBean. It creates a Mock for the EmployeeRepository which can be used to bypass the call to the actual EmployeeRepository:
@RunWith(SpringRunner.class) public class EmployeeServiceImplIntegrationTest { @TestConfiguration static class EmployeeServiceImplTestContextConfiguration { @Bean public EmployeeService employeeService() { return new EmployeeServiceImpl(); } } @Autowired private EmployeeService employeeService; @MockBean private EmployeeRepository employeeRepository; // write test cases here }
@Before public void setUp() { Employee alex = new Employee("alex"); Mockito.when(employeeRepository.findByName(alex.getName())) .thenReturn(alex); }
3. Controller 层:Mock Service层
To test the Controllers, we can use @WebMvcTest. It will auto-configure the Spring MVC infrastructure for our unit tests.
In most of the cases, @WebMvcTest will be limited to bootstrap a single controller. It is used along with @MockBean to provide mock implementations for required dependencies.
@WebMvcTest also auto-configures MockMvc which offers a powerful way of easy testing MVC controllers without starting a full HTTP server.
@RunWith(SpringRunner.class) @WebMvcTest(EmployeeRestController.class) public class EmployeeRestControllerIntegrationTest { @Autowired private MockMvc mvc; @MockBean private EmployeeService service; // write test cases here }
@Test public void givenEmployees_whenGetEmployees_thenReturnJsonArray() throws Exception { Employee alex = new Employee("alex"); List<Employee> allEmployees = Arrays.asList(alex); given(service.getAllEmployees()).willReturn(allEmployees); mvc.perform(get("/api/employees") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(1))) .andExpect(jsonPath("$[0].name", is(alex.getName()))); }
相关推荐
使用JUnit 5的Spring Boot...在撰写本文时,Spring Boot Starter Test引入了JUnit(即JUnit 4)而不是JUnit 5(即 )。 来源 您也可以创建此项目! 使用生成一个项目。 将来自的配置合并到pom.xml 。 利润! 执照 。
Spring Test & Spring Boot Test:为 Spring Boot 应用提供集成测试和工具支持 AssertJ:支持流式断言的 Java 测试框架 Hamcrest:一个匹配器库 Mockito:一个 java mock 框架 JSONassert:一个针对 JSON 的断言...
Spring测试入门 ... Spring Test和Spring Boot Test:对Spring Boot应用程序的实用程序和集成测试支持。 AssertJ :流畅的断言库。 Hamcrest :匹配对象库(也称为约束或谓词)。 Mockito :一个
Use Spring Boot Test framework with JUnit, Cucumber, and Spock Configure and tune web applications and containers Deploy Spring Boot as self-starting executables and Docker containers Monitor data ...
Spring Boot 2 + JUnit 5示例应用程序1.将JUnit 5依赖项添加到pom.xml < dependency>< groupId>org.junit.jupiter</ groupId>< artifactId>junit-jupiter-api</ artifactId>< scope>test</ scope></ dependency>...
创建此示例项目是为了结合 spring-security 讨论正确的 spring-boot 应用程序配置。 当前状态 应用程序的安全配置和集成测试的安全配置。 感谢 Stack Overflow 的 Kris,该应用程序现在按预期工作。 解决方案 如 ...
NULL 博文链接:https://dannyhz.iteye.com/blog/2429129
主要介绍了Spring Boot从Controller层进行单元测试的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
演示-java-hungnguyendang 带有示例应用程序的演示 TDD 设置gradle 设置mongodb 通过以下方式启动 mongodb:#bin/mongod,使用 mongodb 控制台 #bin/mongo 创建数据库 demojavahungnguyen #使用...
Spring Boot 2 REST API的JUnit 5测试用例如何使用Spring Boot Test框架和JUnit 5对RESTful Web服务进行单元测试的示例。它使用MockMvc来提供Spring MVC基础结构,而无需启动HTTP Server。 SpringExtension和@...
这是一个示例Java 8 / Gradle / PostgreSQL / Spring Boot / REST / JUnit应用程序 要求: Java平台(JDK)8, PostgreSQL JUnit 5 Gradle6.0.1 数据库 创建和配置数据库, url, username, password可以在...
包含: Gradle多模块项目Spring Boot 2.0安装CircleCI 2.0基本设置使用MongoDb测试配置JUnit 5测试示例记录配置Gradle配置文件配置Docker撰写随意用于您自己的项目。使用开发人员配置文件运行应用程序./gradlew ...
一个空的Springboot2项目模板, 已包含druid和配置,能启动druid的监控平台 ;包括controller/service/entities和junit test的例子.
旅馆客房管理系统 ...spring-boot-starter-test:提供适用于 Spring Boot 的测试框架,如 JUnit、Hamcrest、Mockito Bootstrap 使用 Bootstrap 作为前端界面框架,支持响应式网页设计;同时使用 jQuery 简化 JavaSc
Spring Boot REST API TDD 演示REST API的Spring Boot应用...jUnit,Hamcrest和Mockito的spring-boot-starter-test 具有单独测试和集成测试源集的Gradle构建系统 ./gradlew test以运行单元测试 ./gradlew integration
主要介绍了Springboot整合junit过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Spring Boot + Apache Cassandra 这是一个Spring Boot + Apache Cassandra应用程序的示例。 它是使用Spring Boot , Apache Cassandra , Spring Security , Spring Data Cassandra , Docker和Docker Compose制...
这是一个Spring Boot Maven演示应用程序,展示了如何使用JUnit和Mockito Framework对Sprig Boot RESTful服务执行完整性和模拟测试。 先决条件 Java SE开发套件8 Maven 3.0+ H2内存数据库(可以与任何其他关系...
结合新的Spring Boot @TestConfiguration您可以提高测试的幸福感。 这个例子的核心存在于我们的PersonControllerIntTest 。 PersonControllerIntTest启动了Spring上下文,因此我们可以对REST端点进行M
SpringBoot+JPA+Freemarker 生成动态SQL,通过Test下的 junit Test运行代码