У меня есть метод @Transactional
, который я должен проверить при сбое транзакции после ее вызова, например:
@Service
public class MyService {
@Transactional
public void myMethod() {
// [...] some code I must run in my test, and throw an exception after it has been called but before the transaction is commited in order for the transaction to be rolled back
}
}
Вот тестовый класс:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApp.class)
public class MyServiceTest {
@SpyBean
private MyService myService;
@Test
public void testMyMethod() {
doAnswer(/* some code which would call the real method, then throw an exception in order to cancel the transaction */)
.when(myService).myMethod();
// [...] other code that test other services when that service failed in the transaction but after its real method has been correctly executed
}
}
Не могли бы вы сказать, какой код добавить в часть /* ... */
в моем тесте?