I'm developing an application which uses AspectJ with Java. In development, I use ajc and java together. AspectJ calls some code segments when necessary and I want to test these code segments called by AspectJ. I tried to do it with Mockito but I failed, does anyone know any other way to test it?
-
Maybe you can try this: https://github.com/david-888/aspectj-junit-runner – David Dec 10 '16 at 19:23
2 Answers
I am not sure on how to do it in plain Java and JUnit, but if you have access to Spring-Integration-Test you can have an easy approach with the MockMVC and support classes that it offers.
And bellow you can see an example in which I am testing a controller that has an Aspect wrapped around it:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration
public class ControllerWithAspectTest {
@Autowired
private WebApplicationContext wac;
@Autowired
private MockMvc mockMvc;
@Autowired
@InjectMocks
private MongoController mongoController;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
// if you want to inject mocks into your controller
MockitoAnnotations.initMocks(this);
}
@Test
public void testControllerWithAspect() throws Exception {
MvcResult result = mockMvc
.perform(
MockMvcRequestBuilders.get("/my/get/url")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
}
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy(proxyTargetClass = true)
static class Config extends WebMvcConfigurerAdapter {
@Bean
public MongoAuditingAspect getAuditingAspect() {
return new MongoAuditingAspect();
}
}
}
You can use the approach above even if you don't have Spring configured in your application, as the approach I've used will allow you to have a configuration class (can and should be a public class residing in it's own file).
And if the @Configuration
class is annotated with @EnableAspectJAutoProxy(proxyTargetClass = true)
, Spring will know that it needs to enable aspects in your test/application.
If you need any extra clarification I will provide it with further edits.
EDIT:
The Maven Spring-Test dependency is:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

- 5,529
- 2
- 33
- 57
-
Well, I'm not into Spring framework but I tried your way. First, spring-webmvc is needed as another dependency because WebAbblicationContext cannot be imported. Even then, there is no MongoAuditingAspect class. As I said, I'm not very into Spring framework so these can be dummy classes, I don't know. I appreciate the help but when I have nothing to do with Spring, I expect to find a nice way to test the code :) – mtyurt Aug 29 '14 at 13:34
-
MongoAuditingAspect was an example of an aspect :) sorry mate i forgot to say that – Bogdan Emil Mariesan Aug 29 '14 at 17:29
I just created a JUnit4 Runner to allow AspectJ Load Time Weaving on JUnit test cases. Here is a simple example:
I created a HelloService to return a greeting. And I created an Aspect to make names in the greeting upper case. Finally i created a unit test to use the HelloService with a lower case name and expect an upper case result.
All the details of the example are part of the GitHub project for reference: https://github.com/david-888/aspectj-junit-runner
Just include the most up to date aspectj-junit-runner JAR in your classpath. Then your tests may look like this:
@AspectJConfig(classpathAdditions = "src/test/hello-resources")
@RunWith(AspectJUnit4Runner.class)
public class HelloTest {
@Test
public void getLiveGreeting() {
String expected = "Hello FRIEND!";
HelloService helloService = new HelloService();
String greeting = helloService.sayHello("friend");
Assert.assertEquals(expected, greeting);
}
}

- 1,204
- 12
- 16