gpt4 book ai didi

java - Spring - 使用 TransactionSynchronizationManager 的测试方法

转载 作者:行者123 更新时间:2023-12-02 11:52:22 25 4
gpt4 key购买 nike

我在我的服务中使用TransactionSynchronizationManager.registerSynchronization(...)并且我无法为此方法编写测试,因为我得到一个异常:java.lang.IllegalStateException:事务同步是不活跃

我的服务的实现:

@Transactional
public void save(Order order) {
log.info("Starting transaction...");
orderDAO.save(order);
TransactionSynchronizationManager.registerSynchronization(
new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
log.info("#afterCommit");
eventPublisher.publishEvent(new OrderPlacedEvent(order));
}
}
);
log.info("Commit");
}

并测试哪个不起作用:

@RunWith(SpringRunner.class)
public class OrderServiceTest {

private OrderDAO orderDAO;
private ApplicationEventPublisher eventPublisher;
private OrderService orderService;

@Before
public void setUp() throws Exception {
orderDAO = mock(OrderDAO.class);
eventPublisher = mock(ApplicationEventPublisher.class);
orderService = new OrderService(
orderDAO,
eventPublisher
);
}

@Test
public void save() throws Exception {
// given
final Order order = new Order(1L, "ORDER");

// when
orderService.save(order);

// then
verify(orderDAO).save(any(Order.class));
verify(eventPublisher).publishEvent(any(OrderPlacedEvent.class));
}

}

和堆栈跟踪:

java.lang.IllegalStateException: Transaction synchronization is not active

at org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(TransactionSynchronizationManager.java:291)
at com.events.eventsdemo.services.OrderService.save(OrderService.java:32)
at com.events.eventsdemo.services.OrderServiceTest.save(OrderServiceTest.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

更新:

我的主类的实现:

@SpringBootApplication
@EnableTransactionManagement
public class TransactionsDemoApplication {

public static void main(String[] args) {
SpringApplication.run(TransactionsDemoApplication.class, args);
}
}

服务:

@Slf4j
@Service
public class OrderService {

private final OrderDAO orderDAO;
private final ApplicationEventPublisher eventPublisher;

@Autowired
public OrderService(OrderDAO orderDAO, ApplicationEventPublisher eventPublisher) {
this.orderDAO = orderDAO;
this.eventPublisher = eventPublisher;
}

@Transactional
public void save(Order order) {
log.info("Starting transaction...");
orderDAO.save(order);
TransactionSynchronizationManager.registerSynchronization(
new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
log.info("#afterCommit");
eventPublisher.publishEvent(new OrderPlacedEvent(order));
}
}
);
log.info("Commit");
}
}

并测试服务:

@RunWith(SpringRunner.class)
@SpringBootTest
public class OrderServiceTest {

@MockBean
private OrderDAO orderDAO;

@MockBean
private ApplicationEventPublisher eventPublisher;

@Autowired
private OrderService orderService;

@Test
public void save() throws Exception {
// given
final Order order = new Order(1L, "ORDER");

// when
orderService.save(order);

// then
verify(orderDAO).save(any(Order.class));
verify(eventPublisher).publishEvent(any(OrderPlacedEvent.class));
}

}

但我遇到了一个异常(exception):

Wanted but not invoked:
org.springframework.context.ApplicationEventPublisher#0 bean.publishEvent(
<any>
);
-> at com.transactions.demo.transactionsdemo.dao.OrderServiceTest.save(OrderServiceTest.java:39)
Actually, there were zero interactions with this mock.

Wanted but not invoked:
org.springframework.context.ApplicationEventPublisher#0 bean.publishEvent(
<any>
);
-> at com.transactions.demo.transactionsdemo.dao.OrderServiceTest.save(OrderServiceTest.java:39)
Actually, there were zero interactions with this mock.

at com.transactions.demo.transactionsdemo.dao.OrderServiceTest.save(OrderServiceTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

也许我应该模拟TransactionSynchronizationAdapter?但我不知道如何注入(inject)该适配器的模拟实现。有什么想法吗?

OrderServiceTest:39 行:verify(eventPublisher).publishEvent(any(OrderPlacedEvent.class));

最佳答案

我必须添加

TransactionSynchronizationManager.initSynchronization();

给我的

@Before
public void init() {

在每次测试之前初始化事务同步的方法

另外,不要忘记在每次之后清除

@After
public void clear() {
TransactionSynchronizationManager.clear();

关于java - Spring - 使用 TransactionSynchronizationManager 的测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47793485/

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