@@ -75,6 +75,7 @@ class TransactionalTransform implements ASTTransformation{
7575 private static final String METHOD_EXECUTE = " execute"
7676 private static final Set<String > METHOD_NAME_EXCLUDES = new HashSet<String > (Arrays . asList(" afterPropertiesSet" , " destroy" ));
7777 private static final Set<String > ANNOTATION_NAME_EXCLUDES = new HashSet<String > (Arrays . asList(PostConstruct . class. getName(), PreDestroy . class. getName(), Transactional . class. getName(), Rollback . class. getName(), " grails.web.controllers.ControllerMethod" , NotTransactional . class. getName()));
78+ private static final Set<String > JUNIT_ANNOTATION_NAMES = new HashSet<String > (Arrays . asList(" org.junit.Before" , " org.junit.After" ));
7879 private static final String SPEC_CLASS = " spock.lang.Specification" ;
7980 public static final String PROPERTY_DATA_SOURCE = " datasource"
8081
@@ -116,7 +117,8 @@ class TransactionalTransform implements ASTTransformation{
116117 for (MethodNode md in methods) {
117118 String methodName = md. getName()
118119 int modifiers = md. modifiers
119- if (! md. isSynthetic() && Modifier . isPublic(modifiers) && ! Modifier . isAbstract(modifiers) && ! Modifier . isStatic(modifiers)) {
120+ if (! md. isSynthetic() && Modifier . isPublic(modifiers) && ! Modifier . isAbstract(modifiers) &&
121+ ! Modifier . isStatic(modifiers) && ! hasJunitAnnotation(md)) {
120122 if (hasExcludedAnnotation(md)) continue
121123
122124 def startsWithSpock = methodName. startsWith(' $spock' )
@@ -138,7 +140,8 @@ class TransactionalTransform implements ASTTransformation{
138140 if (hasAnnotation(md, DelegatingMethod . class)) continue
139141 weaveTransactionalMethod(source, classNode, annotationNode, md);
140142 }
141- else if ((" setup" . equals(methodName) || " cleanup" . equals(methodName)) && isSpockTest(classNode)) {
143+ else if (((" setup" . equals(methodName) || " cleanup" . equals(methodName)) && isSpockTest(classNode)) ||
144+ hasJunitAnnotation(md)) {
142145 def requiresNewTransaction = new AnnotationNode (annotationNode. classNode)
143146 requiresNewTransaction. addMember(" propagation" , new PropertyExpression (new ClassExpression (ClassHelper . make(Propagation . class)), " REQUIRES_NEW" ))
144147 weaveTransactionalMethod(source, classNode, requiresNewTransaction, md, " execute" )
@@ -161,6 +164,17 @@ class TransactionalTransform implements ASTTransformation{
161164 excludedAnnotation
162165 }
163166
167+ private boolean hasJunitAnnotation (MethodNode md ) {
168+ boolean excludedAnnotation = false ;
169+ for (AnnotationNode annotation : md. getAnnotations()) {
170+ if (JUNIT_ANNOTATION_NAMES . contains(annotation. getClassNode(). getName())) {
171+ excludedAnnotation = true ;
172+ break ;
173+ }
174+ }
175+ excludedAnnotation
176+ }
177+
164178 ClassNode getAnnotationClassNode (String annotationName ) {
165179 try {
166180 final classLoader = Thread . currentThread(). contextClassLoader
0 commit comments