[JAVA] How to unit test Spring AOP

Thing you want to do

I want to test a class with cross-business processing created by Spring AOP as a single unit.

How Spring AOP works (roughly) and problems during unit testing

Spring AOP is established based on DI, and the side that uses the component injects a proxy (not the bean itself registered in the container, but the extension of the function defined in AOP for that bean) instance. AOP is realized by. aop.png

However, since I want to do unit testing this time, I would like to provide a method for testing without starting the DI container and depending on the context.

Sample prepared this time

If there is null in the argument, implement the process of throwing NullPointerException in AOP and apply it to the method of TestService (test class) class. (The NullCheck annotation is just a marker, so I'll omit it here.)

AspectLogic.java



@Aspect
@Component
public class AspectLogic {
	@Around("@annotation(NullCheck)")
	public Object invoke(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
		System.out.println("NullCheck!!");
		Stream.of(proceedingJoinPoint.getArgs()).forEach(Objects::requireNonNull);//Get all arguments and Objects::Check with requireNonNull
		return proceedingJoinPoint.proceed();
    }
}

TestService.java



public class TestService {
	@NullCheck
	public void doSomething(Object arg) {
	}
}

Get proxy from AspectJProxyFactory.

Let's implement the part where Spring DI gets proxy from Bean by ourselves. ʻ Instance of AspectJProxyFactory Add the Aspect class created this time to. If you hit the test sample method using the proxy obtained from the factory, it seems that you can confirm that the process defined in AOP is called andNullPointerException` is thrown.

TestAspectLogic.java


public class TestAspectLogic {
	@Test
	public void testAspect() {

		AspectJProxyFactory factory = new AspectJProxyFactory(new TestService());
		factory.addAspect(new AspectLogic()); //Apply AspectLogic class here
		TestService proxy = factory.getProxy();

		try {
			proxy.doSomething(null);
			failBecauseExceptionWasNotThrown(NullPointerException.class);
		} catch(NullPointerException npe) {
		}

	}
}

output


NullCheck!!

Summary

Write a test for AOP as well.

Recommended Posts

How to unit test Spring AOP
How to write a unit test for Spring Boot 2
How to write Spring AOP pointcut specifier
Introduction to Micronaut 2 ~ Unit test ~
Introduction to Spring Boot ② ~ AOP ~
How to unit test with JVM with source using RxAndroid
How to test file upload screen in Spring + Selenium
How to dynamically write iterative test cases using test / unit (Test :: Unit)
How to use Lombok in Spring
How to use Spring Data JDBC
[How to install Spring Data Jpa]
How to set Spring Boot + PostgreSQL
How to use ModelMapper (Spring boot)
[RSpec] How to write test code
ProxyFactory is convenient when you want to test AOP in Spring!
Sample code to unit test a Spring Boot controller with MockMvc
I tested how to use Ruby's test / unit and rock-paper-scissors code.
How to filter JUnit Test in Gradle
Java Artery-Easy to use unit test library
Major changes related to Spring Framework 5.0 Test
How to include Spring Tool in Eclipse 4.6.3?
I want to write a unit test!
How to test private scope with JUnit
[Spring MVC] How to pass path variables
How to write an RSpec controller test
How to split Spring Boot message file
[SpringBoot] How to write a controller test
About Spring AOP
How to deploy
About spring AOP
How to not start Flyway when running unit tests in Spring Boot
[Java] How to test for null with JUnit
About designing Spring Boot and unit test environment
How to use MyBatis2 (iBatis) with Spring Boot 1.4 (Spring 4)
How to test interrupts during Thread.sleep with JUnit
Understand how to share Spring DB connection (DB transaction)
How to use built-in h2db with spring boot
How to use "sign_in" in integration test (RSpec)
How to make Spring Boot Docker Image smaller
JUnit 5: How to write test cases in enum
How to use Spring Boot session attributes (@SessionAttributes)
[Rails] How to implement unit tests for models
How to add a classpath in Spring Boot
Model unit test code to check uniqueness constraints
How to write test code with Basic authentication
How to bind to property file in Spring Boot
How to define multiple orm.xml in Spring4, JPA2.1
[Spring Boot] How to refer to the property file
Spring Boot --How to set session timeout time
How to perform UT with Excel as test data with Spring Boot + JUnit5 + DBUnit
[Rails] From test preparation to model unit testing [RSpec]
How to develop OpenSPIFe
How to set Dependency Injection (DI) for Spring Boot
How to apply HandlerInterceptor to http inbound-gateway of Spring Integration
How to call AmazonSQSAsync
How to use Map
How to write Rails
How to use rbenv
How to use Struts2 * Spring Framework (Spring plugin) June 2017 Version
[Java] How to omit spring constructor injection with Lombok
Introduction to RSpec 1. Test, RSpec