unit test cases for spring aspect by passing join point?

java, spring, spring-aop

Solution

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.

- Use a mocking framework to mock the `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.

Problem

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!

Original source