Reputation: 783
I am trying to write Integratation tests for Custom Aspect. Here is the Aspect Class Snippet.
@Aspect
@Component
public class SampleAspect {
private static Logger log = LoggerFactory.getLogger(SampleAspect.class);
private int count;
public int getCount(){
return count;
}
public void setCount(){
this.count= count;
}
@Around("execution(* org.springframework.data.mongodb.core.MongoOperations.*(..)) || execution(* org.springframework.web.client.RestOperations.*(..))")
public Object intercept(final ProceedingJoinPoint point) throws Throwable {
logger.info("invoked Cutom aspect");
setCount(1);
return point.proceed();
}
}
So the above aspect intercepts whenever jointpoint matches the pointcut. Its working fine. But my question is how to perform Integration test.
What I have done is I created the attribute "count" in Aspect for tracking and asserted it in my Junit. I am not sure if this is good or is there a better way of doing integration testing on aspects.
Here is the snippet of Junit what I have done. I presented in bad way but I hope its undestandable of what I have done for Integration testing.
@Test
public void testSamepleAspect(){
sampleAspect.intercept(mockJointPoint);
Assert.assertEquals(simpleAspect.getCount(),1);
}
Upvotes: 7
Views: 9859
Reputation: 67437
Let us use the same sample code as in my answer to the related AspectJ unit testing question:
Java class to be targeted by aspect:
package de.scrum_master.app;
public class Application {
public void doSomething(int number) {
System.out.println("Doing something with number " + number);
}
}
Aspect under test:
package de.scrum_master.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class SampleAspect {
@Around("execution(* doSomething(int)) && args(number)")
public Object intercept(final ProceedingJoinPoint thisJoinPoint, int number) throws Throwable {
System.out.println(thisJoinPoint + " -> " + number);
if (number < 0)
return thisJoinPoint.proceed(new Object[] { -number });
if (number > 99)
throw new RuntimeException("oops");
return thisJoinPoint.proceed();
}
}
You have several options, depending on what exactly you want to test:
Subsequently I will describe option no. 3. Looking at the sample code above, we see the following side effects:
Integration test for aspect:
package de.scrum_master.aspect;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.matches;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.io.PrintStream;
import org.junit.*;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import de.scrum_master.app.Application;
public class SampleAspectIT {
@Rule public MockitoRule mockitoRule = MockitoJUnit.rule();
private Application application = new Application();
private PrintStream originalSystemOut;
@Mock private PrintStream fakeSystemOut;
@Before
public void setUp() throws Exception {
originalSystemOut = System.out;
System.setOut(fakeSystemOut);
}
@After
public void tearDown() throws Exception {
System.setOut(originalSystemOut);
}
@Test
public void testPositiveSmallNumber() throws Throwable {
application.doSomething(11);
verify(System.out, times(1)).println(matches("execution.*doSomething.* 11"));
verify(System.out, times(1)).println(matches("Doing something with number 11"));
}
@Test
public void testNegativeNumber() throws Throwable {
application.doSomething(-22);
verify(System.out, times(1)).println(matches("execution.*doSomething.* -22"));
verify(System.out, times(1)).println(matches("Doing something with number 22"));
}
@Test(expected = RuntimeException.class)
public void testPositiveLargeNumber() throws Throwable {
try {
application.doSomething(333);
}
catch (Exception e) {
verify(System.out, times(1)).println(matches("execution.*doSomething.* 333"));
verify(System.out, times(0)).println(matches("Doing something with number"));
assertEquals("oops", e.getMessage());
throw e;
}
}
}
Et voilà, we are testing exactly the three types of side effects our sample aspect has by inspecting log output to a mock instance of System.out
and by making sure that the expected exception is thrown for larger positive numbers.
Upvotes: 4
Reputation: 7
@kriegaex What should be the test case implementation for below code
@Aspect
@Component
@Slf4j
public class SampleAspect {
@Value("${timeout:10}")
private long timeout;
@Around("@annotation(com.packagename.TrackExecutionTime)")
public Object intercept( ProceedingJoinPoint point) throws Throwable {
long startTime = System.currentTimeMillis();
Object obj = point.proceed();
long endTime = System.currentTimeMillis();
long timeOut = endTime-startTime;
if(timeOut > timeout)
{
log.error("Error occured");
}
return obj;
}
}
link:Junit-integration test for AOP Spring
Upvotes: -2