0

Issue

I had an Hibernate lazy loading issue when using Postman to send a reqeust derectly to a Springboot service endpoint, which is bacause I have a collection marked as FetchType.Lazy in an entity and this issue can be resolved by marking the service method as @Transactional.

However, if I remove that @Transactional, my Springboot controller test still passed without any Hibernate lazy loading issue.

This looks like all of the Springboot controller integration tests are always transactional. But I couldn't find any official document about it. So, my question is, is it correct?

For my integration test, I use the annotations below

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "classpath:application.properties")
leo
  • 454
  • 5
  • 10
  • 1
    Tests should not be transactional for SpringBootTest by default. The only thing that comes to mind - do you have `spring.jpa.open-in-view=true` configured in application.properties? – Josef Cech Feb 28 '20 at 08:22
  • Thanks @Josef for you comment and that is the exact issue. The explicit config `spring.jpa.open-in-view=false` can solve my problem. I have compared the testing profile with others. The other profiles have truned `spring.jpa.open-in-view` off, But test profile still use the spring default one, which is `spring.jpa.open-in-view=true`. – leo Feb 28 '20 at 13:39

1 Answers1

0

As @Josef said in comment above, my issue here is about default Spring jpa config spring.jpa.oepn-in-view=true. For more details, please read:

leo
  • 454
  • 5
  • 10