-
Notifications
You must be signed in to change notification settings - Fork 38.5k
TestEntityManager can't be configured in @BeforeAll method in test class with @TestInstance(PER_CLASS) in JUnit Jupiter [SPR-16550] #21093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Juergen Hoeller commented I'm afraid |
Juergen Hoeller commented With respect to the exception above, this simply means that a persistence operation was triggered without a transaction being active for the current thread. You should be able to inject a |
Aleksandr commented I thought that |
Juergen Hoeller commented Just looked at your test code, I see where you're coming from here now: You expect all such Sam Brannen, what's your take on this? I suppose |
Aleksandr commented The weird thing, in my point of view, that |
Sam Brannen commented The fact that a The scope of a test-managed transaction is tied to the execution of a test method, test factory, or test template method invocation. Thus it is not possible for Spring to manage a test-managed transaction at the container level (i.e., when |
Aleksandr commented So it's not possible to manage transactions in these methods at all, even in the Lifecycle.PER_CLASS |
Sam Brannen commented
It is part of a well-defined lifecycle; however, similar issues have been raised in the past for TestNG:
So we could potentially revisit #10191, but... I think the use case described in this issue is actually quite different. |
Sam Brannen commented Is your goal to make changes to the database within a test-managed transaction that gets rolled back after the entire class? Or is your goal to make changes to the database in an isolated, committed transaction before all test methods (which potentially have their own isolated, rolled-back transactions)? |
Sam Brannen commented
You can of course manage a transaction manually in such a method -- for example, via Spring's |
Sam Brannen commented For further details, consult the following sections of the reference manual. |
Aleksandr commented
|
Sam Brannen commented OK.... then my earlier assumption about this being a different topic was wrong. In light of that and the upcoming support for scenario tests in JUnit Jupiter (potentially in JUnit Jupiter 5.2), I think it would be prudent to reopen #10191. Consequently, I will close this issue as a duplicate. |
Sam Brannen commented FYI: I have reopened #10191. |
I have the same issues and I'm trying to do exactly what you described above. I want to do a database setup before my tests and then test some stuff. But it don't work with the BeforeAll Method. My Entity class: @Entity
@Table(name = "myTable")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,
property = "id", scope = CampaignEntity.class)
@EqualsAndHashCode
@ToString
@NoArgsConstructor
@Setter
@Getter
public class MyEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@OneToMany(mappedBy = "someName", cascade = CascadeType.MERGE, orphanRemoval = true)
private final Set<DifferentEntity> additionAttr = new HashSet<>();
} My before All method: @BeforeAll
void startUp() {
var myEntities = TestDataGenerator.getListOfEntities(COUNT, ADDITIONAL_ATTR_COUNT);
myRepo.saveAllAndFlush(myEntities); // When I set a debug point here the additionAttr is set with size ADDITIONAL_ATTR_COUNT
} My test, which fails at the assertion: @Test
@Transactional
void myFailingTest() throws Exception {
final MyEntity myEntity = this.myRepo.findAll().get(0);
assertThat(myEntity.getAdditionalAttr()).hasSize(ADDITIONAL_ATTR_COUNT); // here it fails because there is size 0 ...
} Is there a way to fix that issue. it should be possible to do a test setup for the database !? |
Uh oh!
There was an error while loading. Please reload this page.
Aleksandr opened SPR-16550 and commented
When trying to autowire Spring Boot's
TestEntityManager
in method annotated as@BeforeAll
, the following exception is thrown:Other beans are autowired into this method without problems.
The case is following - since I have one test class instance for all tests in it, and they require the same prepopulated DB, why not fill the DB once after test class instantiation?
Sample project is referenced in the issue.
Reference URL: https://gitlab.com/AleksandrSl/spring-sandbox/blob/junit-5-security/src/test/kotlin/pro/parseq/springsandbox/entities/UserItemsSecurityTests.kt
Issue Links:
The text was updated successfully, but these errors were encountered: