I'm saving and loading a @ManyToMany relationship using Hibernate. I have unit tests for these and they are working fine and I can see the records correctly in the database.
I'm attempting to write a JUnit test for updating of these @ManyToMany items.
When I remove @Transactional from the test I get:
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.foo.bar.PrimaryObject.manyToManySet, could not initialize proxy - no Session
But when I add @Transactional to the test I get:
Rolled back transaction for test: [DefaultTestContext@8bb5c2a testClass = PrimaryObjectManagerTest, testInstance = com.foo.bar.dataaccess.PrimaryObjectManagerTest@4d4337f9, testMethod = updatePrimaryObject@PrimaryObjectManagerTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@a5df98c testClass = AnnouncementManagerTest, locations = '{}', classes = '{class com.foo.bar.DataAccessApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@156b88f5, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@71def8f8, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@1da2cb77, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@1e04fa0a, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@5a56cdac, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@36b4cef0], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]
So I cannot verify if there are any real changes to the database!
I've tried adding to the test class:
@RecordApplicationEvents
but that doesn't help.
How can I test an update when I'm dammned if I do and damned if I don't? Thank you!
CodePudding user response:
You can annotate your test method with @Commit
annotation:
@Commit
@Test
@Transactional
void testProcessWithoutRollback() {
// ...
}
Check out this section of the docs for more info: https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#spring-testing-annotation-commit
CodePudding user response:
You can try and annotate your test method with
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void testMethod(String user) {
// ...
}
as far as I remember spring transacts integration tests by default so that after a unit test the database changes can be Rollback to prevent other other unit tests from being affected. Not 100% sure here but it might be worth a try :)