Skip to content

Instantly share code, notes, and snippets.

@tbelmega
Created May 1, 2019 13:35
Show Gist options
  • Save tbelmega/238ac62d7f31c08de1218c80e4214e7c to your computer and use it in GitHub Desktop.
Save tbelmega/238ac62d7f31c08de1218c80e4214e7c to your computer and use it in GitHub Desktop.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner::class)
class ProductE2ESpringBootTest {
@Autowired
lateinit var client: TestRestTemplate
@Autowired
lateinit var repository: ProductRepository
@MockBean
lateinit var authService: AuthService
@MockBean
lateinit var auditLoggerClient: AuditLoggerClient
@Before
@After
fun cleanUpDb() {
repository.deleteAll()
}
@Test
fun `create new product - success - return 201 CREATED`() {
//given
given(authService.isProductAdmin()).willReturn(true)
val createProductRequest = CreateProductRequest("ROG Strix Z390-F Gaming Mainboard", "ASUS")
// when
val response = client.postForEntity(
"/api/v1/products",
createProductRequest,
Void::class.java)
//then
assertThat(response.statusCode).isEqualTo(HttpStatus.CREATED)
assertThat(response.body).isNull()
assertThat(repository.findByProductName(createProductRequest.productName)).hasSize(1)
verify(auditLoggerClient).logProductCreated(createProductRequest)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment