gpt4 book ai didi

java - 在非 Debug模式下,Spring @transaction 在 junit 中无法正常工作

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:30:03 26 4
gpt4 key购买 nike

所有 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 - 在非 Debug模式下,Spring @transaction 在 junit 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28669280/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com