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()

chrislie@xxxxxxxxx


Hi,

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;

@Aspect
@Component
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
stored_procedure()}");
                    callableStatement.execute();

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



--
Sent from: http://aspectj.2085585.n4.nabble.com/AspectJ-users-f2077686.html


Back to the top