LeakLoggingNetworkSession extends NetworkSession { @Override protected void finalize() throws Throwable { logLeakIfNeeded(); super.finalize(); } LeakLoggingNetworkSession( ConnectionProvider connectionProvider, RetryLogic retryLogic, DatabaseName databaseName, AccessMode mode,
            BookmarkHolder bookmarkHolder, long fetchSize, Logging logging );   }
@Test void logsNothingDuringFinalizationIfClosed() throws Exception { Logging logging = mock( Logging.class ); Logger log = mock( Logger.class ); when( logging.getLog( anyString() ) ).thenReturn( log ); LeakLoggingNetworkSession session = newSession( logging, false ); finalize( session ); verify( log, never() ).error( anyString(), any( Throwable.class ) ); }
@Test void logsMessageWithStacktraceDuringFinalizationIfLeaked( TestInfo testInfo ) throws Exception { Logging logging = mock( Logging.class ); Logger log = mock( Logger.class ); when( logging.getLog( anyString() ) ).thenReturn( log ); LeakLoggingNetworkSession session = newSession( logging, true ); session.beginTransactionAsync( TransactionConfig.empty() ); finalize( session ); ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass( String.class ); verify( log ).error( messageCaptor.capture(), any() ); assertEquals( 1, messageCaptor.getAllValues().size() ); String loggedMessage = messageCaptor.getValue(); assertThat( loggedMessage, containsString( "Neo4j Session object leaked" ) ); assertThat( loggedMessage, containsString( "Session was create at" ) ); assertThat( loggedMessage, containsString( getClass().getSimpleName() + "." + testInfo.getTestMethod().get().getName() ) ); }