LazyInitializationException: could not initialize proxy - no Session
Your test should be like this:
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class QuestionTesting {
@Test
public void test() {
}
}
You can add @Transactional
annotation to your test method to avoid this exception.
Method getOne
return the 'reference' (proxy) of the entity which properties can be lazy loaded. See it code - it uses getReference
method of EntityManager
. From it javadoc:
Get an instance, whose state may be lazily fetched.
In Spring the implementation of EntityManager
is org.hibernate.internal.SessionImpl - so without the Session the Spring can not get this method.
To have a session you can just create a transaction...