简体   繁体   English

Junit和Jmock测试Springs TransactionSynchronizationManager

[英]Junit and Jmock to test Springs TransactionSynchronizationManager

I have some code that uses the transaction sychronisation manager.. but i cannot seem to get it work in mocks.. im mocking the entity manager and transaction manager.. so that my context save an entity and call commit... the TransactionSynchronizationManager does not seem to get hit... in the test? 我有一些使用事务同步管理器的代码..但是我似乎无法使其在模拟中工作。.im正在模拟实体管理器和事务管理器..以便我的上下文可以保存实体并调用提交... TransactionSynchronizationManager可以似乎没有受到打击...在测试中?

   this.transactionTemplate.execute(new TransactionCallback<E>() {
                @Override
                public E doInTransaction(TransactionStatus status) {    
                    // update entities


                    TransactionSynchronizationManager.registerSynchronization(new NotificationTransactionSynchronization(){
                       @Override
                       public void afterCommit() {
                    // do some post commit work
                                   int i = notifier.notifyAllListeners();
                       }
                    });

                }
            });

my test class: 我的测试课:

@Test
public void testHappyPath() {


    context.checking(new Expectations() {
        {
            allowing(platformTransactionManager).getTransaction(definition);
            will((returnValue(status)));

            oneOf(platformTransactionManager).commit(status);

                         //next line never gets hit... so the test fails...
                         //if i remove it will pass but i need to check that it works...

            oneOf(mockNotifier).notifyAllListeners();

        }
    });
    this.TestClass.process();
    context.assertIsSatisfied();            
}   

Recently I got to the point where I had to test code which was using transactional hooks and after some investigation I got to following solution: 最近,我到了必须测试使用事务挂钩的代码的位置,经过一番调查,我得到了以下解决方案:

src: src:

public void methodWithTransactionalHooks() {

    //...

    TransactionSynchronizationManager.registerSynchronization(
        new TransactionSynchronizationAdapter() {
            public void afterCommit() { 
                // perform after commit synchronization
            }
        }
    );

    //...
}

test: 测试:

@Transactional
@Test
public void testMethodWithTransactionalHooks() {

    // prepare test

    // fire transaction synchronizations explicitly
    for(TransactionSynchronization transactionSynchronization 
        : TransactionSynchronizationManager.getSynchronizations()
    ){
        transactionSynchornization.afterCommit();
    }

    // verify results
}

Test by default is set to rollback so afterCommit synchronizations won't be fired. 默认情况下,测试设置为回滚,因此afterCommit同步不会被触发。 To test it, explicit call is necessary. 要测试它,显式调用是必要的。

我不确定我是否理解,但是如果您有模拟交易管理器,那么谁来呼叫通知者?

I ran into the same issue, in my case adding 我遇到了同样的问题,以我为例

@Rollback(false)

to the test method helped. 对测试方法有所帮助。

See https://stackoverflow.com/a/9817815/1099376 参见https://stackoverflow.com/a/9817815/1099376

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM