Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transactions to setup / cleanup to restore 1.x behaviour #215

Merged
merged 1 commit into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,46 @@ default void beforeTestClass(TestContext testContext) throws Exception {

}

/**
* Executed before the setup method of a test method.
*
* @param testContext the test context
* @throws Exception allows any exception to propagate
*/
default void beforeSetupTest(TestContext testContext) throws Exception {

}

/**
* Executed after the setup method of a test method.
*
* @param testContext the test context
* @throws Exception allows any exception to propagate
*/
default void afterSetupTest(TestContext testContext) throws Exception {

}

/**
* Executed before the setup method of a test method.
*
* @param testContext the test context
* @throws Exception allows any exception to propagate
*/
default void beforeCleanupTest(TestContext testContext) throws Exception {

}

/**
* Executed after the setup method of a test method.
*
* @param testContext the test context
* @throws Exception allows any exception to propagate
*/
default void afterCleanupTest(TestContext testContext) throws Exception {

}

/**
* Executed before a test method is executed (a test method may contain multiple iterations).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public void beforeTestExecution(TestContext testContext) throws Exception {
fireListeners(TestExecutionListener::beforeTestExecution, testContext);
}

@Override
public void beforeCleanupTest(TestContext testContext) throws Exception {
fireListeners(TestExecutionListener::beforeCleanupTest, testContext);
}

@Override
public void afterCleanupTest(TestContext testContext) throws Exception {
fireListeners(TestExecutionListener::afterCleanupTest, testContext);
}

/** {@inheritDoc} */
@Override
public void afterTestExecution(TestContext testContext) throws Exception {
Expand All @@ -91,6 +101,16 @@ public void afterTestClass(TestContext testContext) throws Exception {
fireListeners(TestExecutionListener::afterTestClass, testContext);
}

@Override
public void beforeSetupTest(TestContext testContext) throws Exception {
fireListeners(TestExecutionListener::beforeSetupTest, testContext);
}

@Override
public void afterSetupTest(TestContext testContext) throws Exception {
fireListeners(TestExecutionListener::afterSetupTest, testContext);
}

/** {@inheritDoc} */
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,35 @@ public class SpringTransactionTestExecutionListener implements TestExecutionList
*/
public SpringTransactionTestExecutionListener(
PlatformTransactionManager transactionManager,
@Property(name = AbstractMicronautExtension.TEST_ROLLBACK) boolean rollback) {
@Property(name = AbstractMicronautExtension.TEST_ROLLBACK, defaultValue = "true") boolean rollback) {

this.transactionManager = transactionManager;
this.rollback = rollback;
}

@Override
public void beforeSetupTest(TestContext testContext) {
beforeTestExecution(testContext);
}

@Override
public void afterSetupTest(TestContext testContext) {
afterTestExecution(false);
}

@Override
public void beforeCleanupTest(TestContext testContext) throws Exception {
beforeTestExecution(testContext);
}

@Override
public void afterCleanupTest(TestContext testContext) throws Exception {
afterTestExecution(false);
}

@Override
public void afterTestExecution(TestContext testContext) {
if (counter.decrementAndGet() == 0) {
if (rollback) {
transactionManager.rollback(tx);
} else {
transactionManager.commit(tx);
}
}
afterTestExecution(this.rollback);
}

@Override
Expand All @@ -74,4 +87,14 @@ public void beforeTestExecution(TestContext testContext) {
tx = transactionManager.getTransaction(new DefaultTransactionDefinition());
}
}

private void afterTestExecution(boolean rollback) {
if (counter.decrementAndGet() == 0) {
if (rollback) {
transactionManager.rollback(tx);
} else {
transactionManager.commit(tx);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import javax.inject.Named;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;

/**
Expand All @@ -41,7 +42,7 @@
* @author graemerocher
* @since 1.0
*/
public class MicronautJunit5Extension extends AbstractMicronautExtension<ExtensionContext> implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, ExecutionCondition, BeforeTestExecutionCallback, AfterTestExecutionCallback, ParameterResolver {
public class MicronautJunit5Extension extends AbstractMicronautExtension<ExtensionContext> implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, ExecutionCondition, BeforeTestExecutionCallback, AfterTestExecutionCallback, ParameterResolver, InvocationInterceptor {
private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(MicronautJunit5Extension.class);

@Override
Expand All @@ -60,6 +61,20 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
beforeTestClass(buildContext(extensionContext));
}

@Override
public void interceptBeforeEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
beforeSetupTest(buildContext(extensionContext));
invocation.proceed();
afterSetupTest(buildContext(extensionContext));
}

@Override
public void interceptAfterEachMethod(Invocation<Void> invocation, ReflectiveInvocationContext<Method> invocationContext, ExtensionContext extensionContext) throws Throwable {
beforeCleanupTest(buildContext(extensionContext));
invocation.proceed();
afterCleanupTest(buildContext(extensionContext));
}

@Override
public void afterAll(ExtensionContext extensionContext) throws Exception {
afterTestClass(buildContext(extensionContext));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import io.micronaut.test.annotation.MicronautTest;
import io.micronaut.test.transaction.spring.SpringTransactionTestExecutionListener;
import javax.inject.Inject;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@MicronautTest(transactional = true)
@DbProperties
Expand All @@ -29,6 +33,16 @@ class TransactionalTest {
@Inject
ApplicationContext applicationContext;

@BeforeEach
void setup() {
Assertions.assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
}

@AfterEach
void cleanup() {
Assertions.assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
}

@Test
void testSpringTransactionListenerMissing() {
Assertions.assertTrue(applicationContext.containsBean(SpringTransactionTestExecutionListener.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void visitSpecAnnotation(MicronautTest annotation, SpecInfo spec) {
spec.addCleanupSpecInterceptor(invocation -> {
afterTestClass(buildContext(invocation, null));
afterClass(invocation);

invocation.proceed();
singletonMocks.clear();
});
Expand All @@ -123,7 +124,12 @@ public void visitSpecAnnotation(MicronautTest annotation, SpecInfo spec) {
for (Object mock : singletonMocks) {
mockUtil.attachMock(mock, (Specification) instance);
}
invocation.proceed();
try {
beforeSetupTest(buildContext(invocation, null));
invocation.proceed();
} finally {
afterSetupTest(buildContext(invocation, null));
}
});

spec.addCleanupInterceptor(invocation -> {
Expand All @@ -135,7 +141,12 @@ public void visitSpecAnnotation(MicronautTest annotation, SpecInfo spec) {
}
creatableMocks.clear();
afterEach(invocation);
invocation.proceed();
try {
beforeCleanupTest(buildContext(invocation, null));
invocation.proceed();
} finally {
afterCleanupTest(buildContext(invocation, null));
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ package io.micronaut.test.spock
import io.micronaut.context.ApplicationContext
import io.micronaut.context.annotation.Property
import io.micronaut.core.util.StringUtils
import io.micronaut.inject.qualifiers.Qualifiers
import io.micronaut.test.annotation.MicronautTest
import io.micronaut.test.spock.entities.Book
import io.micronaut.test.transaction.spring.SpringTransactionTestExecutionListener
import org.springframework.transaction.support.TransactionSynchronizationManager
import spock.lang.Specification
import spock.lang.Stepwise

Expand All @@ -44,6 +44,16 @@ class GormTransactionalRollbackSpec extends Specification {
@Inject
ApplicationContext applicationContext

def setup() {
// check transaction is present in setup
assert TransactionSynchronizationManager.isSynchronizationActive()
}

def cleanup() {
// check transaction is present in setup
assert TransactionSynchronizationManager.isSynchronizationActive()
}

void "bean SpringTransactionTestExecutionListener exists"() {
expect:
applicationContext.containsBean(SpringTransactionTestExecutionListener)
Expand Down