Estou tendo o seguinte problema: Quando rodo a aplicação localmente, tudo funciona. Porém, quando rodo o teste, os métodos que interagem com o redis não funcionam. parece que não estão conseguindo se comunicar com o container criado no teste. se eu deixo o redis que uso em desenvolvimento ativo, os métodos funcionam. o que pode ser? parece que não está trocando o endereço nas properties durante os testes.
Classe Rating Controller test:
@Testcontainers @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class RatingControllerTest : DatabaseContainerConfiguration() {
@Autowired
private lateinit var webApplicationContext: WebApplicationContext
private lateinit var mockMvc: MockMvc
private var token: String? = null
@Autowired
private lateinit var jwtUtil: JWTUtil
companion object {
private const val END_POINT_RATING = "/rating"
private const val END_POINT_RATING_ID = END_POINT_RATING.plus("/%s")
}
@BeforeEach
fun setup() {
token = generateTokenForTest()
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.apply<DefaultMockMvcBuilder?>(
SecurityMockMvcConfigurers.springSecurity()
).build()
}
@Test
fun `should return code 200 when rating is found`() {
mockMvc.get(END_POINT_RATING_ID.format(1)).andExpect { status { is2xxSuccessful() } }
}
@Test
fun `teste df fdsf `() {
mockMvc.get(END_POINT_RATING).andExpect { status { is2xxSuccessful() } }
}
@Test
fun `should return code 400 when request without token`() {
mockMvc.delete(END_POINT_RATING_ID.format(1)).andExpect { status { is4xxClientError() } }
}
@Test
fun `should return code 400 when rating not found`() {
mockMvc.get(END_POINT_RATING_ID.format(99)).andExpect { status { is4xxClientError() } }
}
@Test
fun `should return code 200 when correct token`() {
mockMvc.delete("/rating/1") {
headers { token?.let { this.setBearerAuth(it) } }
}.andExpect { status { is2xxSuccessful() } }
}
private fun generateTokenForTest(): String? {
val authorities = mutableListOf(br.com.leonardo.dicionarioAPI.model.Role(id = 1, "READ_WRITE"))
return jwtUtil.generateToken("leonardo_spring_boot@gmail.com", authorities = authorities)
}
}
Arquivo application: spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mysql://localhost:3306/dictionary_api_database spring.datasource.username=spring_boot spring.datasource.password=Leonardodev@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.jpa.show-sql = true
spring.cache.type=redis spring.redis.host=localhost spring.redis.port=6379
spring.mail.host=sandbox.smtp.mailtrap.io spring.mail.port=2525 spring.mail.username=fcf96dc63ef45f spring.mail.password=4791b5e2b9a482 spring.mail.properties.mail.transport.protocol=smtp spring.mail.properties.mail.debug=true spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.transport.smtp.starttls.enable=true
jwt.secret = secret