Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Unit test AspectJ which pointcut Datasource getConnection()



I have created aspect below which basically pointcut everytime we get a new
connection from DB and then run some stored procedure on that connection. I
have manually tested it and it is working fine. Now how do i unit test this
using Junit and/or mockito ?

If i mock a connection with something like 
Connection connection = Mockito.mock(Connection.class); then it doesn't
serve the purpose as i am mocking the connection. Basically i want to check
that this aspect works when every time we get a new connection (and not mock
connection ) . Any thoughts ??

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;

public class GetConnectionAspect {

    @AfterReturning(pointcut = "execution (*
javax.sql.DataSource.getConnection(..))", returning = "connection")
    public void interceptConnection(Connection connection) throws
SQLException {
                CallableStatement callableStatement = null;
                try {
                    callableStatement = connection.prepareCall("{call

                } catch (SQLException e) {
                    System.out.println("Exception occurred while executing
the stored procedure" +  e);
                } finally {
                    if(callableStatement != null){

Sent from:

Back to the top