Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unit test cases for spring aspect by passing join point?

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!

like image 851
user755806 Avatar asked Apr 08 '13 13:04

user755806


People also ask

What is the easiest method to write a unit test in spring?

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.

Do you use Spring in a unit test?

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.

What is Spring AOP example?

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.


1 Answers

I think you have a couple of good options here...

  1. 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.
  2. 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.

like image 200
nicholas.hauschild Avatar answered Oct 03 '22 00:10

nicholas.hauschild