[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [aspectj-users] Unit test AspectJ which pointcut Datasource getConnection()
|
Hi,
it would be helpful to get some sample code from your target class. I
cannot write a test for you or help you write one if I have nothing to
run it against. Ideally, please provide an MCVE
(http://stackoverflow.com/help/mcve).
Sorry, I am unfamiliar with database programming and a quick sample I
created with H2 according to the tutorial at
http://zetcode.com/java/h2database/ does not use
javax.sql.DataSource.getConnection(..)
but
DriverManager.getConnection(url, user, passwd)
thus the aspect does not fire. I tried it your way, but after a few
minutes I could not get it running because of an exception like
javax.naming.NoInitialContextException: Need to specify class name
in environment or system property, or as an applet parameter, or in
an application resource file: java.naming.factory.initial
Then I remembered that actually it is not my job to provide an MCVE,
interested as I might be in helping you. So please help me get running
code first, then I can inspect your testing and mocking situation.
Regards
--
Alexander Kriegisch
https://scrum-master.de
Chrislie schrieb am 22.01.2019 05:57:
> 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
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> To change your delivery options, retrieve your password, or unsubscribe from
> this list, visit
> https://www.eclipse.org/mailman/listinfo/aspectj-users
>