File tree Expand file tree Collapse file tree
core-java-modules/core-java-jndi/src/test/java/com/baeldung/jndi Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .baeldung .jndi ;
22
3+ import org .junit .jupiter .api .AfterAll ;
34import org .junit .jupiter .api .BeforeAll ;
45import org .junit .jupiter .api .Test ;
56import org .springframework .jdbc .datasource .DriverManagerDataSource ;
@@ -56,5 +57,11 @@ void givenContext_whenLookupByName_thenValidDataSource() throws Exception {
5657 assertNotNull (ds );
5758 assertNotNull (ds .getConnection ());
5859 }
60+
61+ @ AfterAll
62+ static void tearDown () throws Exception {
63+ ctx .close ();
64+ }
65+
5966
6067}
Original file line number Diff line number Diff line change 66import javax .naming .NameNotFoundException ;
77import javax .naming .NoInitialContextException ;
88
9- import org .junit .jupiter .api .Disabled ;
109import org .junit .jupiter .api .MethodOrderer ;
1110import org .junit .jupiter .api .Order ;
1211import org .junit .jupiter .api .Test ;
1615
1716@ TestMethodOrder (MethodOrderer .OrderAnnotation .class )
1817public class JndiExceptionsUnitTest {
18+
19+ InitialContext ctx ;
1920
2021 @ Test
2122 @ Order (1 )
2223 void givenNoContext_whenLookupObject_thenThrowNoInitialContext () {
2324 assertThrows (NoInitialContextException .class , () -> {
2425 JndiTemplate jndiTemplate = new JndiTemplate ();
25- InitialContext ctx = (InitialContext ) jndiTemplate .getContext ();
26+ ctx = (InitialContext ) jndiTemplate .getContext ();
2627 ctx .lookup ("java:comp/env/jdbc/datasource" );
28+ ctx .close ();
2729 }).printStackTrace ();
2830 }
2931
@@ -35,8 +37,9 @@ void givenEmptyContext_whenLookupNotBounds_thenThrowNameNotFound() {
3537 builder .activate ();
3638
3739 JndiTemplate jndiTemplate = new JndiTemplate ();
38- InitialContext ctx = (InitialContext ) jndiTemplate .getContext ();
40+ ctx = (InitialContext ) jndiTemplate .getContext ();
3941 ctx .lookup ("badJndiName" );
42+ ctx .close ();
4043 }).printStackTrace ();
4144 }
4245
You can’t perform that action at this time.
0 commit comments