Unit testing with AspectJ in Java Baeldung
I am developing an application that uses AspectJ from Java. In development I use ajc and java together. AspectJ names some code segments when needed and I want to test those code segments called AspectJ. I tried to do this with Mockito but I failed, does anyone know another way to test it?
source to share
I'm not sure how to do this in plain Java and JUnit , but if you have access to Spring-Integration-Test you can easily approach MockMVC and support the classes it offers.
And below you can see an example where I am testing a controller with 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 above approach even if you don't have Spring configured in your application, as the approach I am using will allow you to have a config class (can and should be a public class in its own file).
And if the class is @Configuration
annotated with @EnableAspectJAutoProxy(proxyTargetClass = true)
, Spring will know to include aspects in your test / application.
If you need further clarification, I'll give it further changes.
EDIT:
Maven Spring-Test Dependency:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
source to share
I just created a JUnit4 Runner to allow AspectJ Load Time Weaving on JUnit test cases. Here's a simple example:
I created a HelloService to return a greeting. And I created an Aspect to create uppercase greetings. Finally, I created a unit test to use the HelloService in lower case and expect an upper case result.
All example details are part of the GitHub project for reference: https://github.com/david-888/aspectj-junit-runner
Just include the latest aspectj-junit-runner JAR in your classpath . Then your tests might 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);
}
}
source to share