Need to create JNDI names for JUnit use of hibernate
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   David_Smith
Posted On:   Monday, December 17, 2007 12:12 PM

I'm looking to use JUnit to test some code that uses Hibernate to connect to my DB (SQL Server). Although I can do this using a hibernate.cfg.xml file I want to create a super class I can extend so I won't have to rewrite code for each TestCase subclass. To make a long story short I'm trying to define some JNDI properties that will include my DB name, username, and password so hibernate can pick it up. I think I have the basics in my setUp method, but I keep getting a NoIntialContextException. My method is below: protected void setUp() throws Exception { Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "javax.naming.spi.InitialContextFactory");    More>>

I'm looking to use JUnit to test some code that uses Hibernate to connect to my DB (SQL Server). Although I can do this using a hibernate.cfg.xml file I want to create a super class I can extend so I won't have to rewrite code for each TestCase subclass. To make a long story short I'm trying to define some JNDI properties that will include my DB name, username, and password so hibernate can pick it up. I think I have the basics in my setUp method, but I keep getting a NoIntialContextException. My method is below:




protected void setUp() throws Exception {

Properties env = new Properties();


env.put(Context.INITIAL_CONTEXT_FACTORY, "javax.naming.spi.InitialContextFactory");

env.put("connection.url", "jdbc:sqlserver://localhost:1433;databaseName=DevDB;userName=u53rn@m e;password=p@55w0rd;SelectMethod=cursor");

Context ctx = new InitialContext(env);

}



I can't seem to figure out what I'm doing wrong. All the tutorial I've been looking at have not helped. Any ideas?

   <<Less

Re: Need to create JNDI names for JUnit use of hibernate

Posted By:   Joseph_Hobbs  
Posted On:   Monday, February 4, 2008 10:58 AM

I went down the same path a while back, trying to connect to a database to test my classes. Unfortunately I also realized I couldn't control the order my test cases ran. This meant I couldn't guarantee the data's integrity and my tests were difficult to work with.



I ended up using DBUnit to get around this issue. DBUnit can be used to automatically populate a database with 'test' data that gets reloaded before each single test. This allows each test to run on a known set of starting data, and updates/deletes won't break tests if they run out of order. Since the data is always reloaded before a test, it's clean.



With all that said, I created my own TestCase object that includes all the DBUnit code. So I simply subclass this test and write my specific tests there. From there I just configured Hibernate to hit the database using the hibernate-cfg.xml as normal.



If you're interested I can post pertinent source for your reference. It doesn't address your original issue, but will give you a pretty flexible alternative...

Re: Need to create JNDI names for JUnit use of hibernate

Posted By:   Anonymous  
Posted On:   Wednesday, January 16, 2008 12:24 PM

Check out this post:
http://blogs.sun.com/randystuph/entry/injecting_jndi_datasources_for_junit
Basically you have to create the initial context in your test class, create the hierarchy and inject the datasource of your choice.

HTH

-- Randy

Re: Need to create JNDI names for JUnit use of hibernate

Posted By:   Christopher_Koenigsberg  
Posted On:   Saturday, December 22, 2007 04:41 PM

Are you trying to get a JNDI initialContext from a sqlserver? hmm, I think you need a JNDI server, first.... and then when you get your initialcontext from the JNDI server, you use it to look up the information about how to get a DataSource for the SQLServer.
About | Sitemap | Contact