Skip to content

Commit af20a79

Browse files
committed
close jndi context
1 parent 53f00df commit af20a79

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

core-java-modules/core-java-jndi/src/test/java/com/baeldung/jndi/JndiUnitTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.baeldung.jndi;
22

3+
import org.junit.jupiter.api.AfterAll;
34
import org.junit.jupiter.api.BeforeAll;
45
import org.junit.jupiter.api.Test;
56
import 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
}

core-java-modules/core-java-jndi/src/test/java/com/baeldung/jndi/exceptions/JndiExceptionsUnitTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import javax.naming.NameNotFoundException;
77
import javax.naming.NoInitialContextException;
88

9-
import org.junit.jupiter.api.Disabled;
109
import org.junit.jupiter.api.MethodOrderer;
1110
import org.junit.jupiter.api.Order;
1211
import org.junit.jupiter.api.Test;
@@ -16,14 +15,17 @@
1615

1716
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1817
public 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

0 commit comments

Comments
 (0)