Add unit tests

This commit is contained in:
azhwani 2020-05-26 15:34:41 +01:00
parent 33ba33867f
commit 954d6b5be9
2 changed files with 69 additions and 0 deletions

View File

@ -0,0 +1,37 @@
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.BeforeEach;
import org.junit.jupiter.api.Test;
public class SimpleJNDIUnitTest {
private InitialContext initContext;
@BeforeEach
public void setup() throws Exception {
System.setProperty("java.naming.factory.initial", "org.osjava.sj.SimpleContextFactory");
System.setProperty("org.osjava.sj.root", "file://src/test/resources");
System.setProperty("org.osjava.sj.delimiter", ".");
System.setProperty("jndi.syntax.separator", "/");
System.setProperty("org.osjava.sj.space", "java:/comp/env");
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());
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.jndi.datasource;
import static org.junit.Assert.assertNotNull;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.mock.jndi.SimpleNamingContextBuilder;
@SuppressWarnings("deprecation")
public class SimpleNamingContextBuilderUnitTest {
private InitialContext initContext;
@BeforeEach
public void init() throws Exception {
SimpleNamingContextBuilder.emptyActivatedContextBuilder();
this.initContext = new InitialContext();
}
@Test
public void whenMockJndiDataSource_thenReturnJndiDataSource() throws Exception {
this.initContext.bind("java:comp/env/jdbc/datasource", new DriverManagerDataSource("jdbc:h2:mem:testdb"));
DataSource ds = (DataSource) this.initContext.lookup("java:comp/env/jdbc/datasource");
assertNotNull(ds.getConnection());
}
}