Mvc Test with mocks是集成测试还是单元测试
MvcTest with mocks is integration test or unit test
如果我们在休息端点的 MvcMock
测试中有模拟,我们可以将其称为 integration test
吗?当然,我可以说 TestRestTemplate
测试是 integration test
因为当我测试端点时没有模拟。
MvcMock test
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
internal class CreateProductRestControllerMvcTest {
@Autowired
private lateinit var mockMvc: MockMvc
@Autowired
private lateinit var objectMapper: ObjectMapper
@MockkBean
private lateinit var createProductService: CreateProductService
@ParameterizedTest
@ArgumentsSource(ValidCreateProductRequests::class)
internal fun `POST a new Product, GIVEN without any exception, THEN return Product id and status 201`(request: CreateProductRequest) {
// Given
every { createProductService.invoke(request.toCommand()) } returns ProductId(1L)
// When, Then
this.mockMvc.post(REST_PRODUCTS) {
contentType = APPLICATION_JSON
content = objectMapper.writeValueAsString(request)
}
.andExpect {
status { isCreated() }
content { json((objectMapper.writeValueAsString(1L))) }
}
}
}
TestRestTemplate Test
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = [Application::class])
@ActiveProfiles("test")
@Testcontainers
@FlywayTest
@ExtendWith(FlywayTestExtension::class)
class CreateProductRestControllerIntegrationTest {
@Autowired
private lateinit var restTemplate: TestRestTemplate
private lateinit var headers: HttpHeaders
@BeforeEach
internal fun setUp() {
headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_JSON
}
@FlywayTest
@ParameterizedTest
@ArgumentsSource(ValidJsonRequestBodies::class)
internal fun `POST new Product, GIVEN valid request, THEN returns 201`(json: String) {
// Given
val request: HttpEntity<String> = HttpEntity(json, headers)
// When
val result = restTemplate.postForEntity(REST_PRODUCTS, request, String::class.java);
// Then
assertNotNull(result.body)
assertEquals(HttpStatus.CREATED, result.statusCode)
}
}
在我看来,将其称为“集成测试”并不完全公平。通过模拟服务,您基本上是在测试您的控制器并测试序列化和反序列化的能力 JSON,这是一个完全有效的测试。
在 Spring 世界中,您可能会发现这些被称为“切片测试”,测试的重点是检查应用程序的给定层,而不是完全成熟的集成测试。有关它们的更多信息:
如果我们在休息端点的 MvcMock
测试中有模拟,我们可以将其称为 integration test
吗?当然,我可以说 TestRestTemplate
测试是 integration test
因为当我测试端点时没有模拟。
MvcMock test
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
internal class CreateProductRestControllerMvcTest {
@Autowired
private lateinit var mockMvc: MockMvc
@Autowired
private lateinit var objectMapper: ObjectMapper
@MockkBean
private lateinit var createProductService: CreateProductService
@ParameterizedTest
@ArgumentsSource(ValidCreateProductRequests::class)
internal fun `POST a new Product, GIVEN without any exception, THEN return Product id and status 201`(request: CreateProductRequest) {
// Given
every { createProductService.invoke(request.toCommand()) } returns ProductId(1L)
// When, Then
this.mockMvc.post(REST_PRODUCTS) {
contentType = APPLICATION_JSON
content = objectMapper.writeValueAsString(request)
}
.andExpect {
status { isCreated() }
content { json((objectMapper.writeValueAsString(1L))) }
}
}
}
TestRestTemplate Test
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = [Application::class])
@ActiveProfiles("test")
@Testcontainers
@FlywayTest
@ExtendWith(FlywayTestExtension::class)
class CreateProductRestControllerIntegrationTest {
@Autowired
private lateinit var restTemplate: TestRestTemplate
private lateinit var headers: HttpHeaders
@BeforeEach
internal fun setUp() {
headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_JSON
}
@FlywayTest
@ParameterizedTest
@ArgumentsSource(ValidJsonRequestBodies::class)
internal fun `POST new Product, GIVEN valid request, THEN returns 201`(json: String) {
// Given
val request: HttpEntity<String> = HttpEntity(json, headers)
// When
val result = restTemplate.postForEntity(REST_PRODUCTS, request, String::class.java);
// Then
assertNotNull(result.body)
assertEquals(HttpStatus.CREATED, result.statusCode)
}
}
在我看来,将其称为“集成测试”并不完全公平。通过模拟服务,您基本上是在测试您的控制器并测试序列化和反序列化的能力 JSON,这是一个完全有效的测试。
在 Spring 世界中,您可能会发现这些被称为“切片测试”,测试的重点是检查应用程序的给定层,而不是完全成熟的集成测试。有关它们的更多信息: