 |
Using mock naming contexts for testing
Posted by simongbrown on November 21, 2003 at 06:48 AM | Comments (7)
Say for example that you want to unit test a Service Locator - a class that looks up data sources, topics, queues, etc from JNDI. How would you go about doing this?
One option would be to simply setup a JNDI environment inside a J2EE application server and write some JUnit tests to run inside the container. While this works, ideally you may want your unit tests to run independently and quickly.
The option we've just taken is to use a Mock Objects approach, therefore enabling us to run the tests during our normal unit test cycle. The one tiny problem that we ran into was that the service locator itself is responsible for creating a naming context by creating a new InitialContext instance, meaning that it's harder to get a mock context in there. The solution? Just write a mock InitialContextFactory as follows.
import javax.naming.Context;
import javax.naming.spi.InitialContextFactory;
/**
* Provides a JNDI initial context factory for the MockContext.
*/
public class MockInitialContextFactory implements InitialContextFactory {
public Context getInitialContext(Hashtable env) {
return new MockContext();
}
}
Then, when you want to run your tests, just stick a jndi.properties file on your classpath like this.
java.naming.factory.initial=some.package.mock.MockInitialContextFactory
At runtime, the call to new InitialContext() uses the mock factory and results in the creation of a mock context. Just remember not to deploy the properties file in production!
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment
-
Mocking JNDI
I ran across this issue in a recent project. One thing I discovered is that there is a distinction between "mocks" that act as infrastructure (you don't care to validate communication with them) and mocks for collaborative classes (that you do want to validate). I call the first category harnesses.
In any case, I solved the same problem and avoided the use of a property file. Do something like this:
public class ContextHarness implements Context {
public static class Factory implements InitialContextFactory {
private static Context ctx;
public Context getInitialContext(Hashtable environment) throws
NamingException {
return ctx;
}
protected static void setContext(Context context) {ctx = context;} // not in inteface
}
public ContextHarness() {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
Factory.class.getName());
Factory.setContext(this);
}
// plus your implementations of Context methods
// a HashMap was sufficient for my needs
}
To use it, your test setup can just call new ContextHarness() then bind whatever mocks are needed to it. The actual code under test only has to call new InitialContext() to receive a reference to it.
Posted by: gvaughn on November 21, 2003 at 08:52 AM
-
Mocking JNDI
I've discovered commons-naming from the jakarta-commons sandbox.
It has a fairly decent in memory JNDI implementation, and provides factories for common resources. It would look very familiar to Tomcat users.
I recently added some changes so that it can be initialised from an XML configuration file. This can make setting up the environment much easier.
Posted by: brettporter on November 24, 2003 at 02:07 PM
-
Mocking JNDI
I've done the similar thing, but used property file as an JNDI url. Property is in the format as used by Sun's FS-based JNDI implementation whith the only difference that there are an additional mappings for subcontexts.
Posted by: euxx on November 27, 2003 at 09:06 AM
-
Many thanks - I've just come across the same issue. This works a treat.
Posted by: mgladdish on March 22, 2005 at 03:18 AM
-
There is also Spring's SimpleNamingContext (org.springframework.mock.jndi.SimpleNamingContext) that uses a simple hashset.
Posted by: kvalcanti on October 18, 2007 at 01:16 PM
-
wow power leveling
wow powerleveling
wow power leveling
wow gold
wow items
feelingame.com
wow tips
Most Valuable WOW Power Leveling Service
wow power leveling faq
cheap wow power leveling
wow power leveling
wow powerleveling
wow power lvl
Posted by: wowleveling3 on December 13, 2007 at 06:45 PM
-
网络营销软件
网络营销软件
网络营销软件
群发软件
群发软件
---
群发软件
网络营销软件
论坛群发软件
网站排名软件
群发软件
推广小助手破解版
论坛群发软件
网站排名软件
群发软件
推荐给你很好的群发软件和信息群发软件和供求群发软件
推荐给你很好的群发软件和信息群发软件和供求群发软件博客群发软件网络营销软件网络营销软件
网站排名软件网站排名软件网站优化软件信息群发软件信息群发软件信息群发软件论坛群发软件网站推广软件网站推广软件博客群发软件博客群发软件
群发软件群发软件博客群发软件论坛群发软件网络营销软件论坛群发软件
信息群发软件推广软件网站推广软件网络营销软件网站推广软件群发软件网站排名软件网站推广软件博客群发软件论坛群发软件群发软件网站排名软件网站推广软件博客群发软件论坛群发软件
网站排名软件
博客群发软件
网站排名软件
网站推广软件
群发软件信息群发软件
免费论坛群发软件
论坛群发软件
网站排名软件
免费博客群发软件
网站推广软件
群发软件
博客群发软件
网站排名软件
网站推广软件
群发软件信息群发软件
免费论坛群发软件
论坛群发软件
网站排名软件
免费博客群发软件
博客群发软件
信息群发软件
论坛群发软件
信息群发软件
博客群发软件
qq群发软件
邮件群发软件
博客群建软件
企业名录搜索软件
信息群发软件
邮件群发软件
论坛群发软件
博客群发软件
网站推广软件
网络营销软件
全能营销破解版
网络营销软件
论坛群发软件
论坛群发软件
论坛群发软件
网络营销软件
信息群发软件
信息群发软件
信息群发软件
群发软件
论坛群发软件
网络营销软件
网络营销软件
网络营销软件
群发软件
群发软件
---网络营销软件
网站推广软件
群发软件
Posted by: info0089099 on December 25, 2007 at 04:54 PM
|