【发布时间】:2021-11-07 12:08:24
【问题描述】:
如果我们在休息端点上的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)
}
}
【问题讨论】:
标签: spring-boot unit-testing kotlin integration-testing junit5