Typically, when testing an application that uses JNDI, we may want to use a mocked datasource instead of a real one. This is a common practice when testing in order to make our unit tests simple and fully separated from any external context.
In this tutorial, we’ll showcase how to test a mock JNDI datasource using the Spring Framework and the Simple-JNDI library.
In short, JNDI binds logical names to external resources like database connections. The main idea is that the application doesn’t have to know anything about the defined datasource except its JNDI name.
Simply put, all naming operations are relative to a context, so to use JNDI to access a naming service, we need to create an InitialContext object first. As the name implies the InitialContext class encapsulates the initial (root) context that provides the starting point for naming operations.
In simple words, the root context acts as an entry point. Without it, JNDI can’t bind or lookup our resources.
Spring provides out-of-box integration with JNDI through SimpleNamingContextBuilder. This helper class offers a great way to mock a JNDI environment for testing purposes.
So, let’s see how we can use the SimpleNamingContextBuilder class to unit test a JNDI datasource.
First, we need to build an initial naming context for binding and retrieving the datasource object:
@BeforeEach
public void init() throws Exception {
SimpleNamingContextBuilder.emptyActivatedContextBuilder();
this.initContext = new InitialContext();
}
We’ve created the root context using the emptyActivatedContextBuilder() method because it provides more flexibility over the constructor, as it creates a new builder or returns the existing one.
Now that we have a context, let’s implement a unit test to see how to store and retrieve a JDBC DataSource object using JNDI:
@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());
}
As we can see_,_ we use the_ bind()_ method to map our JDBC DataSource object to the name java:comp/env/jdbc/datasource.
Then we use the lookup() method to retrieve a DataSource reference from our JNDI context using the exact logical name that we used previously to bind the JDBC DataSource object.
Note that, JNDI will simply throw an exception in case the specified object is not found in the context.
It’s worth mentioning that the_ SimpleNamingContextBuilder_ class is deprecated since Spring 5.2 in favor of other solutions such as Simple-JNDI.
#spring-framework #java #testing #developer