java – 在非调试模式下,Spring @transaction在junit中没有按预期工作

前端之家收集整理的这篇文章主要介绍了java – 在非调试模式下,Spring @transaction在junit中没有按预期工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
所有MyService方法都是事务性的.下面的junit测试,获取项目数,保存新项目,并获取项目数,以确保计数增加1.
public class MyTest extends ServiceTest{

    1. int countBefore = myService.getCount();    //return n
    2. myService.add(item);                       //item is really added to DB
    3. int countAfter  = myService.getCount();    //return n (sometimes n+1)
}

@Transactional(propagation=Propagation.REQUIRES_NEW,isolation=Isolation.READ_COMMITTED)
getCount(){…}

@Transactional(propagation=Propagation.REQUIRES_NEW,isolation=Isolation.SERIALIZABLE)
add(){…}
@Ignore
@ContextConfiguration(locations = { "file:src/main/resources/xxx-context.xml","file:src/main/resources/xxx-data.xml","file:src/main/resources/xxx-services.xml"  })
@TransactionConfiguration(transactionManager = "txManager",defaultRollback = false)
@TestExecutionListeners( {  DependencyInjectionTestExecutionListener.class,DirtiesContextTestExecutionListener.class,TransactionalTestExecutionListener.class,TestListener.class})

public class ServiceTest extends AbstractUT{
@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners( {TestListener.class})
public class AbstractUT{

调试时(3.)返回n 1,这就是我想要的.但是在没有调试的情况下运行测试时我会得到n.

甚至有时在运行测试时我得到n 1,下次我得到n并且当比较两次执行之间的std输出时,它看起来完全一样.我启用了log4j.logger.org.springframework.transaction = TRACE,我可以看到:

Initializing transaction synchronization
Getting transaction for MyService.getCount
...
Completing transaction for MyService.getCount
Clearing transaction synchronization

...

Initializing transaction synchronization
Getting transaction for MyService.add
...
Completing transaction for MyService.add
Clearing transaction synchronization

...

Initializing transaction synchronization
Getting transaction for MyService.getCount
...
Completing transaction for MyService.getCount
Clearing transaction synchronization

因此交易一个接一个地执行,但是(3.)怎么可能看不到保存的项目?

交易管理在我的测试类中设置为:https://stackoverflow.com/a/28657650/353985

我怎样才能找到出错的地方?
谢谢!

解决方法

有类似的问题,但在我的情况下,它没有回滚.好像你忘记添加@Transactional了.来自文档( link)

交易管理

In the TestContext framework,transactions are managed by the
TransactionalTestExecutionListener which is configured by default,
even if you do not explicitly declare @TestExecutionListeners on your
test class. To enable support for transactions,however,you must
configure a PlatformTransactionManager bean in the ApplicationContext
that is loaded via @ContextConfiguration semantics (further details
are provided below). In addition,you must declare Spring’s
@Transactional annotation either at the class or method level for your
tests.

以下是上面链接的示例.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration(transactionManager="txMgr",defaultRollback=false)
@Transactional
public class FictitIoUsTransactionalTest {

@BeforeTransaction
public void verifyInitialDatabaseState() {
    // logic to verify the initial state before a transaction is started
}

@Before
public void setUpTestDataWithinTransaction() {
    // set up test data within the transaction
}

@Test
// overrides the class-level defaultRollback setting
@Rollback(true)
public void modifyDatabaseWithinTransaction() {
    // logic which uses the test data and modifies database state
}

@After
public void tearDownWithinTransaction() {
    // execute "tear down" logic within the transaction
}

@AfterTransaction
public void verifyFinalDatabaseState() {
    // logic to verify the final state after transaction has rolled back
}

}

猜你在找的Java相关文章