I am working with:
- Spring Framework 4.3.2
- AspectJ 1.8.9
- JUnit
- Gradle
The project is based in multi-modules.
In src/main/java
(main) I have some @Aspect
classes and they work how is expected. I can confirm it through Runtime and Testing
Now I need for JUnit through logging show the @Test
method name that is executed
Therefore in src/test/java
(test) I have the following:
class TestPointcut {
@Pointcut("execution(@org.junit.Test * *())")
public void testPointcut(){}
}
@Aspect
@Component
public class TestAspect {
private static final Logger logger = LoggerFactory.getLogger(TestAspect.class.getSimpleName());
@Before(value="TestPointcut.testPointcut()")
public void beforeAdviceTest(JoinPoint joinPoint){
logger.info("beforeAdviceTest - Test: {} - @Test: {}", joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName() );
}
}
Observe the second class has @Aspect
and @Component
therefore it is recognized by Spring
Note: I can confirm that If I write wrong the @Pointcut
syntax or expression I get errors.
The problem is when I execute my @Test
methods, For the TestAspect
class the @Before
advice never works.
I did a research in Google and I have seen that the @Pointcut("execution(@org.junit.Test * *())")
pattern is correct.
Even If I use a more explicit such as: @Pointcut(value="execution(public void com.manuel.jordan.controller.persona.*Test.*Test())")
, it does not work.
Consider I have the following for Gradle
project(':web-27-rest') {
description 'Web - Rest'
dependencies {
compile project(':web-27-service-api')
testRuntime project(':web-27-aop')
testRuntime project(':web-27-aop').sourceSets.test.output
What is missing or wrong?
Alpha:
One kind of Test classes are:
- Server side working with
@Parameters
and@ClassRule
+@Rule
Therefore:
@RunWith(Parameterized.class)
@ContextConfiguration(classes={RootApplicationContext.class})
@Transactional
public class PersonaServiceImplTest {
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE= new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Autowired
private PersonaService personaServiceImpl;
...
@Parameters
public static Collection<Persona[]> data() {
.....
});
}
...
@Test
@Sql(scripts={"classpath:....-script.sql"})
public void saveOneTest(){
....
}
Other are:
- Web side working with (
@WebAppConfiguration
) and either:- with
@Parameters
and@ClassRule
+@Rule
- without
@Parameters
and@ClassRule
+@Rule
- with
Therefore (below the second approach):
@Transactional
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={RootApplicationContext.class, ServletApplicationContext.class})
public class PersonaDeleteOneControllerTest {
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
private ResultActions resultActions;
...
@BeforeClass
public static void setUp_(){
...
}
@Before
public void setUp(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void deleteOneHtmlGetTest() throws Exception {