Hi Stale,
Couldn't get it any smaller, :)
Cut down Session Bean
public class AOPUnitTestManagerBean {
|
| private IPersistenceHelper helper;
|
| public AOPUnitTestManagerBean() {
| helper = new PersistenceHelper(null);
| }
|
| public void createObject(Object o) {
| helper.create(o);
| }
| }
JUnit Test
import org.junit.Before;
| import org.junit.Test;
|
| public class AOPUnitTestManagerBeanTest {
|
| private AOPUnitTestManagerBean man;
|
| @Before
| public void setUp() throws Exception {
| man = new AOPUnitTestManagerBean();
| }
|
| @Test
| public void testCreateObject() {
|
| String s = new String("Hello");
| man.createObject(s);
| }
| }
IPersistenceHelper interface
public interface IPersistenceHelper {
|
| public void create(Object o);
| }
MockPersistenceHelper
public class MockPersistenceHelper implements IPersistenceHelper {
|
| public MockPersistenceHelper(String PERSISTENCE_CONTEXT_NAME) {
| }
|
| public void create(Object o) {
| System.out.println("inside MockPersistenceHelper.create()");
| }
| }
PersistenceHelper
public class PersistenceHelper implements IPersistenceHelper {
|
| public PersistenceHelper(String persistenceContextName) {
| }
|
| public void create(Object o) {
| System.out.println("inside PersistenceHelper.create()");
| System.out.println("This should have been mocked-out --- did you set
-javaagent in jvm args?");
| }
| }
Interceptor
import org.jboss.aop.advice.Interceptor;
| import org.jboss.aop.joinpoint.Invocation;
|
| public class PersistenceHelperInterceptor implements Interceptor {
|
| public String getName() {
| return "PersistenceHelperInterceptor";
| }
|
| public Object invoke(Invocation invocation) throws Throwable {
| System.out.println("Inside PersistenceHelperInterceptor.invoke");
|
| Object helper = new MockPersistenceHelper(null);
| return helper;
| }
| }
jboss-aop.xml
<?xml version="1.0" encoding="UTF-8"?>
| <aop>
|
| <bind pointcut="execution(public
$typedef{persistenceHelperTypeDef}->new(java.lang.String))">
| <interceptor class="PersistenceHelperInterceptor"/>
| </bind>
|
| <!-- This causes Stack Overflow Error since both PersistenceHelper
| and MockPersistenceHelper implement IPersistenceHelper, causing
| infinite loop (of sorts) -->
| <typedef name="persistenceHelperTypeDef"
expr="class($instanceof{IPersistenceHelper})" />
|
| <!-- This causes ClassCastException -->
| <!-- typedef name="persistenceHelperTypeDef"
expr="class($instanceof{IPersistenceHelper}) AND !class(MockPersistenceHelper)"
/-->
|
| </aop>
Hope this helps
View the original post :
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4215632#...
Reply to the post :
http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&a...