我在我的服务TransactionSynchronizationManager.registerSynchronization(...)
中使用,我无法为此方法编写测试,因为我得到一个例外:java.lang.IllegalStateException: Transaction synchronization is not active
。
我的服务实施:
@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));
}
}
和stacktrace:
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));
}
}
但我得到一个例外:
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
?但我不知道如何注入这个适配器的模拟实现。有什么想法吗?
OrderServiceTest:39是行:verify(eventPublisher).publishEvent(any(OrderPlacedEvent.class));
答案 0 :(得分:2)
我必须添加
TransactionSynchronizationManager.initSynchronization();
给我的
@Before
public void init() {
为了在每次测试之前初始化事务同步的方法
另外,不要忘记在每个之后清除
@After
public void clear() {
TransactionSynchronizationManager.clear();
答案 1 :(得分:1)
添加@EnableTransactionManagement
。
修改强>
这对我来说很好......
@SpringBootApplication
@EnableTransactionManagement
public class So47793485Application {
public static void main(String[] args) {
SpringApplication.run(So47793485Application.class, args);
}
@Component
public static class Foo {
@Transactional
public void foo() {
TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
@Override
public void afterCommit() {
System.out.println("here");
}
@Override
public void afterCompletion(int status) {
System.out.println(
status == TransactionSynchronization.STATUS_COMMITTED ? "committed" : "rolled back");
}
});
}
}
}
和
@RunWith(SpringRunner.class)
@SpringBootTest
public class So47793485ApplicationTests {
@Autowired
Foo foo;
@Test
// @Transactional
public void contextLoads() {
foo.foo();
}
}
当我在测试中取消注释@Transactional
时,tx会回滚。