2

I want to add some common code (eg. log the name of the test to logback) to my testng tests via AOP. The following is my Aspect & test code:

@Aspect
@Component
public class TestAspect {
...
@Around(value = "execution(* *(..)) && @annotation(org.testng.annotations.Test)")
public Object test(@NotNull final ProceedingJoinPoint pjp) {
...
}}


@ContextConfiguration(classes = { mysample.SpringTestConfigurator.class })
@WebAppConfiguration
public class GlobalTest extends AbstractTestNGSpringContextTests {
...
@Test(enabled = true, priority = 1)
public void testOS() {
...
}}

But the Aspect is not called. Where is the problem ?

kulatamicuda
  • 1,603
  • 2
  • 21
  • 40
  • please share your SpringTestConfigurator. You can also turn spring logs on to see if it process anything connected with aspects. – hi_my_name_is Sep 29 '14 at 09:52
  • SpringTest configurator configures various things like LDAP and properties. The only relevated AOP configuration here is @EnableAspectJAutoProxy . Strange is that other Aspects works as expected, it seems to me that AOP is not applied on the test methods only, but on the called service methods it does. How can I turn on more detailed spring logs ? – kulatamicuda Sep 30 '14 at 07:28
  • depends if you use logback.xml or log4j.properties. In the latter case try to add log4j.logger.org.springframework=DEBUG entry – hi_my_name_is Sep 30 '14 at 08:23
  • I am using logback, from log it seems to me that it has something to do with Spring not proxying GlobalTest. I solved it another way but still want to know if it is solvable via Spring "native AOP" without LTW or CTW. – kulatamicuda Sep 30 '14 at 12:08

1 Answers1

2

I am not a Spring user, but what I do know is that Spring AOP is only able to intercept methods of Spring Beans as described in the Spring documentation. So unless your test class is a Spring Bean, it will not work. With full AspectJ it would.

Now you have two options: Either make your test a Spring Bean (which means that the test itself needs the Spring infrastructure to work, which is not so nice) or you use AspectJ for intercepting test methods. I would recommend the latter, but - your code, your decision. :-)

kriegaex
  • 63,017
  • 15
  • 111
  • 202