What we've done in testing that has worked well for us is have surefire set up to use the aspectj agent in the runs and then used spock to tests the side effects of the aspects being woven.
There's a really simple project I have on github that screws with the platform MBean server and makes it return fake instances of various mbeans. In practice, this project on github is not really good for anything, but it does show off one way of testing aspects using spock - for the spock tests I have simple tests like:
In other places we've used AOP to do more complicated things, we will similarly test the side effects of the aspects being applied. For instance, in one place where we are using aspects a lot for monitoring, our unit tests have some simple constructs to test that after hitting the aspects that the stats are increased in our stats registry as we would expect them to be:
def "test weaving of java.sql.CallableStatement.executeUpdate(String,int[])"() {
setup:
CallableStatement statement = Mock()
when:
statement.executeUpdate('UPDATE TABLE', [0] as int[])
then:
def newSize = old(MetricsRegistry.getInstance().getMetrics().size()) + 1
MetricsRegistry.getInstance().getMetrics().size() == newSize
}
Using spock has helped us a lot because the aspectj weaver will, in most cases, let us use simple spock mocks and have them woven (in the above case the mock for the CallableStatement is woven by the aspectj agent so that the call to "executeUpdate" has our aspects woven around it).
In other places, we will test various pieces of the aspects themselves by mocking up joinpoints and passing them in and making sure that the aspects themselves behave as we want them to behave.
Using spock for testing aspectj has, for us, been very intuitive because run our setup, put a simple statement in our "when" block that matches something we intend to weave, and then in our "then" block make sure that any side-effects of our aspect happened.
Ryan