我正在努力获得一些我写的回滚交易的测试。我的测试通过并且正常工作。但是,当我尝试使其成为事务性(因此我可以回滚测试)时,我收到以下错误
java.lang.IllegalStateException: Cannot start a new transaction without ending the existing transaction.
at org.springframework.test.context.transaction.TransactionalTestExecutionListener.beforeTestMethod(TransactionalTestExecutionListener.java:175) ~[spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:249) [spring-test-4.1.6.RELEASE.jar:4.1.6.RELEASE]
at
如果我在路由中抛出异常,它将撤消任何数据库保存,因此我所拥有的事务确实有效......它似乎不会将自己附加到测试和回滚中?
我的测试看起来像这样。
@RunWith(CamelSpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {TestAppConfig.class})
@Transactional
public class RouteTest
{
@Autowired SignUpRoute signUpRoute;
@Test
public void basicTest() throws Exception{
ModelCamelContext signUpRouteContext = signUpRoute.getContext();
MessageDto messageDto = new MessageDto();
messageDto.setFirstName();
messageDto.setLastName();
// etc
ObjectMapper mapper = new ObjectMapper();
String jsonMessage = mapper.writeValueAsString(messageDto);
String rabbitURL = "rabbitmq://docker:5672/ignUp.exchange?queue=...etc...";
ProducerTemplate producerTemplate = supplierSignUpRouteContext.createProducerTemplate();
producerTemplate.setDefaultEndpoint(END_POINT_MOCK);
producerTemplate.sendBody(rabbitmqURL, jsonMessage);
END_POINT_MOCK.expectedMessageCount(1);
END_POINT_MOCK.assertIsSatisfied();
}
}