Announcement Announcement Module
Collapse
No announcement yet.
Trouble using SimpleNamingContextBuilder in integration test to create datasource Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble using SimpleNamingContextBuilder in integration test to create datasource

    In my context, I have a handful of elements like this:
    Code:
    <jee:jndi-lookup jndi-name="jdbc/someds" id="someDataSource"/>
    In my appserver, I'll configure a datasource with this JNDI name, and that should be fine. However, in my integration test, I have to manually create this JNDI binding.

    In my integration test class, I have code like this:
    Code:
        @Before
        public void init() {
            SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
            DataSource  someds  =
                    new DriverManagerDataSource(System.getProperty("test.some.url"),
                                                System.getProperty("test.some.userName"),
                                                System.getProperty("test.some.password"));
            builder.bind("jdbc/someds", someds);
        }
    When I run this, I see errors like this:
    Cannot resolve reference to bean 'someDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'someDataSource': Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
    Is this happening because it's trying to initialize the context before my "init()" method has executed?
    Is there something else I have to do in the "init()" method (or elsewhere) to make this work?

  • #2
    I noticed this: http://blog.jonschneider.com/2009/05...p-can-see.html , which seems to indicate that I need to set the "java.naming.factory.initial" property (perhaps in system properties) to an appropriate factory class name. I don't know what that would be in this case, being an integration test not running in my server container.

    Comment


    • #3
      You need to execute that block in a method that runs on class init not before a test method because the context is already build before that. So put it in a method annotated with @BeforeClass (and make it static).

      Comment


      • #4
        Good idea, but it didn't help. I even tried setting a breakpoint at the start of the method and running it in debug, but it doesn't hit the breakpoint, as it's failing before it gets there. I then tried setting breakpoints in SpringJUnit4ClassRunner, and it tries to initialize the context before it executes my "init()" method.

        The start of my method now looks like this:
        Code:
            @BeforeClass
            public static void init() {
                SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();

        Comment


        • #5
          I would have thought an alternative would be to remove the "RunWith" and "ContextConfiguration" annotations, and then manually instantiate the context after loading the datasource. For some reason that I don't understand, this had very little impact. It still seems like it's using the SpringJUnit4ClassRunner, and the result was the same.

          Comment


          • #6
            If your class is extending one of the spring based classes those annotations are already on the super classes.

            Comment

            Working...
            X