Is there any sort of pre-when block functionality that exists? #2154
Replies: 3 comments 2 replies
-
Spock 2.4-M5 introduced IBlockListener which might be handy in your case (PR: #1575). Btw, to test that feature, I created an extension which calls "flush" on |
Beta Was this translation helpful? Give feedback.
-
Does it have to be right before a If you just need it be called before each iteration, you can simply have a base class for your specs where you have a Or if you don't want to use a base class, you can also write a simple extension that adds an iteration interceptor or feature method intercepter or whichever is appropriate for your case where you do the call (https://spockframework.org/spock/docs/2.3/all_in_one.html#_interceptors). This could either be a global extension to work on each and every feature, or you can make it as annotation driven extension that only works on annotated specs or features. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the global extension idea. This seems to be working: class EntityManagerClearingGlobalExtension implements IGlobalExtension {
private static final IBlockListener BLOCK_LISTENER = new EntityManagerClearingWhereBlockListener()
@Override
void visitSpec(SpecInfo spec) {
boolean isRepoIntegrationSpec = BaseRepositoryIntegrationSpec.isAssignableFrom(spec.reflection)
if (!isRepoIntegrationSpec) {
return
}
spec.features.forEach { it -> it.addBlockListener(BLOCK_LISTENER) }
}
private static class EntityManagerClearingWhereBlockListener implements IBlockListener {
@Override
void blockEntered(Specification spec, BlockInfo blockInfo) {
if (blockInfo.kind != BlockKind.WHEN) {
return
}
if (!(spec instanceof BaseRepositoryIntegrationSpec)) {
return
}
spec.entityManager.clear()
}
}
} And here's the base repo spec: @DataJpaTest
@CompileStatic
@ActiveProfiles('local')
@AutoConfigureTestDatabase(replace = NONE)
@SuppressWarnings('AbstractClassWithoutAbstractMethod')
abstract class BaseRepositoryIntegrationSpec extends Specification {
@Autowired
protected EntityManager entityManager
} |
Beta Was this translation helpful? Give feedback.
-
Asking because we have Spring Data Repo tests and it would be really nice to just have a base repo test that calls
entityManager.clear()
before thewhen
block to make sure we are actually testing the repo vs the database, not just the 1st level JPA/Hibernate cache.Beta Was this translation helpful? Give feedback.
All reactions