0

I try to make my test to work with Spring @Transactional annotation.

@ContextConfiguration(classes = SomeTest.SomeTestSpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SomeTest {

    @Autowired
    MyBean some;

    @Autowired
    PlatformTransactionManager transactionManager;

    @Test
    public void testSpring() throws Exception {
        some.method();
        assertTrue(some.isTransactionalWorks);
    }

    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @EnableLoadTimeWeaving
    @EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
    @TransactionConfiguration
    static class SomeTestSpringConfig {

        @Bean
        PlatformTransactionManager transactionManager() {
            return new MyTransactionManager(dataSource());
        }

        @Bean
        MyBean some() {
            return new MyBean();
        }

        @Bean
        DataSource dataSource() {
            return new SimpleDriverDataSource(Driver.load(), "jdbc:h2:mem:unit-test");
        }
    }
}

class MyBean {

    @Autowired
    DataSource dataSource;

    public boolean isTransactionalWorks;

    @Transactional
    private void someInTransaction() {
        try {
            dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("I should be in transaction");
    }

    public void method() {
        someInTransaction();
    }
}


class MyTransactionManager implements PlatformTransactionManager, InitializingBean {
    private final DataSourceTransactionManager base = new DataSourceTransactionManager();

    @Autowired
    MyBean some;

    public MyTransactionManager(DataSource datasource) {
        base.setDataSource(datasource);
    }

    @Override
    public TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException {
        some.isTransactionalWorks = true;
        return base.getTransaction(definition);
    }

    @Override
    public void commit(TransactionStatus status) throws TransactionException {
        base.commit(status);
    }

    @Override
    public void rollback(TransactionStatus status) throws TransactionException {
        base.rollback(status);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        base.afterPropertiesSet();
    }
}

Also I added -javaagent:D:/libs/spring-instrument-4.1.7.RELEASE.jar to VM options for this test. But it always fails. What did I miss?

turbanoff
  • 2,439
  • 6
  • 42
  • 99
  • 1
    Please check this link, i think it is the similar problem u are facing. http://stackoverflow.com/questions/13162582/how-to-configure-aspectj-with-load-time-weaving-without-interface In this link he has asked to provide both aspectjweaver.jar and spring-instrument.jar in vm argument – Sanket Bajoria Oct 01 '15 at 19:25
  • @SanketBajoria Bingo! You are right. Please add your comment as answer. I will accept it. – turbanoff Oct 01 '15 at 19:53

1 Answers1

0

Please check this link, i think it is the similar problem u are facing.

How to configure AspectJ with Load Time Weaving without Interface

In this link he has asked to provide both aspectjweaver.jar and spring-instrument.jar in vm argument.

Good to know it worked. :)

Community
  • 1
  • 1
Sanket Bajoria
  • 748
  • 6
  • 18