updating jndi.properties

This commit is contained in:
azhwani 2020-06-10 18:46:08 +01:00
parent ecf97d79ea
commit b9befff7b5
2 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package com.baeldung.jndi.datasource;
import static org.junit.Assert.assertEquals;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class SimpleJNDIUnitTest {
private InitialContext initContext;
@BeforeEach
public void setup() throws Exception {
this.initContext = new InitialContext();
}
@Test
public void whenMockJndiDataSource_thenReturnJndiDataSource() throws Exception {
String dsString = "org.h2.Driver::::jdbc:jdbc:h2:mem:testdb::::sa";
Context envContext = (Context) this.initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("datasource/ds");
assertEquals(dsString, ds.toString());
}
@AfterEach
public void tearDown() throws Exception {
if(this.initContext != null){
this.initContext.close();
this.initContext = null;
}
}
}

View File

@ -5,6 +5,7 @@ import static org.junit.Assert.assertNotNull;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
@ -28,5 +29,13 @@ public class SimpleNamingContextBuilderUnitTest {
assertNotNull(ds.getConnection());
}
@AfterEach
public void tearDown() throws Exception {
if(this.initContext != null){
this.initContext.close();
this.initContext = null;
}
}
}