0

非常に単純なテスト シナリオを機能させることができません。論理的には(私にとって)そうすべきですが、そうではありません

私はトランザクションサービスを持っています:

@Transactional(propagation = Propagation.REQUIRED)
public void addCustomer(Customer newCustomer)  {
    customerDAO.insert(newCustomer);
}

したがって、トランザクションを開始するか、既存のトランザクションを再利用するように設定されています。アプリのコンテキストでtx:annotation-driven transaction-manager="transactionManager"設定したので、Spring はこのサービスをトランザクションとして認識します。

このコードを呼び出すテストがあり、正常に動作します。

しかし、ロールバックを検証する別のテストが必要でした。そこで、TranscationTemplate ブロックを呼び出すテストを作成しました。

   final int preCount = service.getCustomerCount();;

    TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
    transactionTemplate.execute(new TransactionCallback<Void>() {
        public Void doInTransaction(TransactionStatus status) {

                // adding another and rolling back
                Customer c = new Customer();
                                    .
                                    .
                  service.addCustomer(c);

                status.setRollbackOnly();
                throw new RuntimeException("rollback");

このブロックとサービスの両方が (?) 同じトランザクションを共有する必要があるため、これにより addCustomer サービス呼び出しがロールバックされると想定しました。

しかし、ロールバックは決して起こりません

誰でも理由はありますか?プログラム型トランザクションと宣言型トランザクションを共有できませんか?

編集> ターゲット DB は H2 です。

Junit は次のように定義されます。

RunWith(SpringJUnit4ClassRunner.class)

ContextConfiguration(locations = {"/customer-application-context.xml"})

public class TestAddCustomer extends AbstractTransactionalJUnit4SpringContextTests 

transactionManager はクラスで自動配線されます。

4

0 に答える 0