https://kotlinlang.org logo
#kotest
Title
# kotest
i

Ido Flax

06/15/2022, 1:35 PM
Hello ppl. Is there anyway to get transactions to rollback for tests in kotest + spring without using annotationSpec?
l

LeoColman

06/15/2022, 10:42 PM
How do you normally rollback spring transactions?
t

thanksforallthefish

06/16/2022, 6:00 AM
so you have an example? we simply use a Spring project extension (https://kotest.io/docs/extensions/spring.html) and annotate the test class, eg:
Copy code
class ProjectConfig : AbstractProjectConfig() {
  override fun extensions() = listOf(
    SpringExtension,
    SpringAutowireConstructorExtension,
    ... more extensions
  )

  ...
}

@AutoConfigureMockMvc
@SpringBootTest
@Transactional
class MyIntegrationTest(
    private val mockMvc: MockMvc,
    private val testMother: TestMother,
    private val messageOutboxRepository: MessageOutboxRepository,
) : StringSpec()
each test is then rollbacked (because
@Transactional
annotation)
i

Ido Flax

06/16/2022, 6:14 AM
Copy code
@DataJpaTest
@ImportAutoConfiguration(RefreshAutoConfiguration::class)
@ContextConfiguration(
    classes = [HiveConfig::class, TableIsDBConfig::class],
    initializers = [DataAccessTestInitializer::class]
)
@ActiveProfiles("integration-test, qa")
@ComponentScan(basePackages = ["com.bla"])
abstract class DataAccessTest : FeatureSpec() {
//spring extension here (i already removed kotest from my pom)
}
t

thanksforallthefish

06/16/2022, 6:18 AM
looks to me you are just missing the
@Transactional
annotation in there, then it should work. I don’t use test slices, but I think
@DataJpaTest
configures everything related to persistence, so transaction should definitely work (I don’t use test slices because coincidentally I think they make your test suite slower, since the application context is memoized if all tests share the same context it is initializated only once, if you start using test slices you get multiple contexts, data, web, jackson, etc etc)
i

Ido Flax

06/16/2022, 6:39 AM
@thanksforallthefish I thought i had @Transactional on it. weird. I’ll try again with kotest and feature spec. Re slices, yeah i get you, but in this case i’m writing tests for a data access module so it only does DA
Seems i had the transactional annotation on the tests themselves instead of the class. i think that was the issue
Scratch that, still no transaction rollback
t

thanksforallthefish

06/16/2022, 7:13 AM
try to import
TransactionAutoConfiguration
as well, but if you say that with annotationspec transactions work it is probably not that
at this point though, I am honestly out of ideas, I would need to see your project (and have the time, which is also a luxury in this period)
i

Ido Flax

06/16/2022, 7:37 AM
Copy code
@DataJpaTest(
    properties = [
        "spring.application.name=datamatic-tableis-IT",
        "spring.cloud.bootstrap.enabled=false",
        "atlas.rest.address=<http://localhost:10200>"
    ]
)
@ImportAutoConfiguration(RefreshAutoConfiguration::class)
@ContextConfiguration(
    classes = [HiveConfig::class, TableIsDBConfig::class],
    initializers = [DataAccessTestInitializer::class]
)
@ActiveProfiles("integration-test, qa")
@ComponentScan(basePackages = ["com.bla"])
@Transactional
abstract class DataAccessTest : AnnotationSpec(){
    override fun extensions() = listOf(SpringExtension)
}
Here’s my current setup, which doesn’t seem to work
it seems to be rolling back for test failures though:
Copy code
[org.springframework.test.context.transaction.TransactionContext()] - Rolled back transaction for test:
I might be wrong. looking into it
Ok i think the issue is that i was expecting it to rollback DDL statements, which in mysql are not transactional. mwep 😕
t

thanksforallthefish

06/16/2022, 8:20 AM
curiosity, why would you want DDL rollbacked? that would make your tests super slow, since all tables would need to be recreated on each test? anyway, good you found out 👍
169 Views