i have spring aop aspect and it has method as below:it is an aspect which will be called before particular method.
public void someMethod(JoinPoint jp) {
//Somelogic
}
i need to write a junit test case for this. how can i instantiate and send parameeters to someMethod
? i mean how can i populate JoinPoint
with args? how can set parameters in joint point and pass it? aspect is configured in application context xml file.
Thanks!
Spring Boot simplifies unit testing with SpringRunner . It is also easier to write unit tests for REST Controller with MockMVC . For writing a unit test, Spring Boot Starter Test dependency should be added to your build configuration file (pom.
Spring is a framework. A framework calls you, so by definition, using spring with your controller is an integration point and does not belong in a unit test.
AOP is like triggers in programming languages such as Perl, . NET, Java, and others. Spring AOP module provides interceptors to intercept an application. For example, when a method is executed, you can add extra functionality before or after the method execution.
I think you have a couple of good options here...
JoinPoint
is an interface. Because of this, you should be able to create your own 'stub' implementation that has the behavior you expect. In your test, instantiate a new instance of this stub and pass it into the someMethod
method.JoinPoint
object, and setup expected behaviors on the mock. This might sound somewhat foreign if you are not familiar with mocking frameworks in general, so I would recommend taking a look at this documentation on Mockito, which is a popular mocking framework.When it comes to unit testing, best practice (mine at least) suggests that a test should always use a mock/stub of some sort (for members and passed parameters) to ensure that the unit test is testing just the unit. If you were to use the implementation of JoinPoint
that Spring/AspectJ uses, then you might also be testing some logic of their implementation in your unit test. If those implementation details change in the future, your test may break without any code changes from you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With