Hi all
I tried to test my classes in Web tier of J2EE application
and got some kind of problem.
For example, in some class (Helper) I have a method that
connects to EJB tier with Delegate class (using Business Delegate pattern)
public class Helper.... {
public
void someMethod() {
try
{
Delegate
delegate = new Delegate();
//
using delegate to invoke business methods of EJB
//
.....
} catch(
DelegateException e) {
// exception in delegate
//....
}
}
.....................
}
Here is the Delegate class
public class Delegate {
public Delegate()
throws DelegateException {
try {
//
getting home interface of ejb using JNDI and storing in local variable
//.....
}
catch(EJBException
e) {
throw new DelegateException()
}
....
}
//
... business methods delegating to the actual enterprise bean
//
...
}
To test Helper class (out-of-container testing) I try to
isolate it from any problems that can arise in Delegate class (with JNDI and
EJBs for example)
So I wrote MockDelegate class that extends Delegate
public class MockDelegate extends Delegate {
public
MockDelegate() throws DelegateException {
}
//
my stubs for all needed business methods
//
...
}
Then I tried to use AspectJ to replace the creating of Delegate
in Helper (with around advice)
Delegate delegate = new Delegate();
with
Delegate delegate = new MockDelegate();
But this was silly because the default constructor of Delegate
in any case is executed when MockDelegate object is created
And I get DelegateException from Delegate constructor because
of JNDI and EJB dependency what I tried to avoid.
I don't know can I replace the body of constructor of
Delegate class to do nothing (and how?)
Or can I replace the line
Delegate delegate = new Delegate();
With
MockDelegate delegate = new MockDelegate()
?
(I may refactor Helper class but I may not refactor Delegate
class.)
Can you give me any advice what is the best way to do this?
Any help will be much appreciated!
Thanks
Leonid Visochin
E-mail: LVissochin@xxxxxxxxxxxxx
www.luxoft.com