I am using Spring Boot version 2.3.12.RELEASE.
Tests that use TestEntityManager are failing with the following error:
java.lang.IllegalStateException: No transactional EntityManager found
Sample test:
@DataJpaTest
@Transactional
@AutoConfigureTestEntityManager
public class SubscriptionRepositoryTest {
@Autowired
private SubscriptionRepository subscriptionRepository;
@Autowired
private TestEntityManager entityManager;
@Nested
class FindUserActiveSubscription {
@Test
public void shouldReturnSubscription_whenUserHasActiveSubscription() {
Subscription subscription = new Subscription();
subscription.setId("1");
subscription.setUserId("testuserid");
subscription.setEndDate(LocalDateTime.now().plusDays(1));
subscription.setStatus(SubscriptionStatus.ACTIVE);
entityManager.persist(subscription);
subscription = subscriptionRepository.findUserActiveSubscription("testuserid");
Assertions.assertNotNull(subscription);
}
}
}
Comment From: snicoll
@ShaimaaSabry thanks for the report but a partial code snippet is not enough for us to figure out what is going on. Can you please move that code in text in an actual project we can run to reproduce the problem? We can share it with us as a zip attached to this issue or using a link to a GitHub repository.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.
Comment From: myin142
Since I don't know if this counts as a bug, I will just post it here in case someone stumbles upon this. @Transactional
annotation only applies for the annotated class, not it's inner classes. But annotating the inner class is not enough to make it work. I don't have a better solution than just removing the inner class.