Skip to main content

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

I just opened my sample project and saw this open case in it. I was a
bit bored, so I took some more time in order to recreate your situation
without an application server, using simple-jndi in my stand-alone Java
program in order to emulate the DataSource.getConnection(..) call. BTW,
I still think it would have been your job to provide working sample
code, as I said. I also think it was not very nice to ignore my request
because just because you are lazy to help me help you.

As a side note, for me the aspect only fires with call(), not with
execution(). If it works for you anyway, I conclude that you must be
using load-time weaving (LTW). Anyway, it does not make a big difference
for the question at hand.

So you said you wanted to run a JUnit test against a working database
connection, not a mocked one. But then you do not write a unit test but
an integration test. Is this what you want? And if so, what are you
trying to test? That weaving actually works? I do not think it makes a
lot of sense to test the weaving mechanism. I think you should test your
own aspect as such in a unit test. An integration test on a high level,
such as checking that the aspect alters a result in contrast to running
the application without aspect might make sense, but as I said, you
should not test AspectJ but rather your aspect. So which one do you
want? Please provide some feedback, then I can help you take the next
step. I just need to know the purpose of your test first.

Regards
-- 
Alexander Kriegisch
https://scrum-master.de


Alexander Kriegisch schrieb am 23.01.2019 20:16:

> 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.
> 
> 
> Chrislie schrieb am 22.01.2019 05:57:
> 
>> 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();
>>                    }
>>                }
>>            }
>>        }



Back to the top