views:

759

answers:

3

Newcomer to Spring here, so pardon me if this is a stupid question.

I have a relatively small Java library that implements a few dozen beans (no database or GUI). I have created a Spring Bean configuration file that other Java projects use to inject my beans into their stuff.

I am now for the first time trying to use Spring Test to inject some of these beans into my junit test classes (rather than simply instantiating them).

I am doing this partly to learn Spring Test and partly to force the tests to use the same bean configuration file I provide for others.

In the Spring documentation is says I need to create an application context using the "TestContext" class that comes with Spring. I believe this should be done in a spring XML file that I reference via the @ContextConfiguration annotation on my test class.

@ContextConfiguration({"/test-applicationContext.xml"})

However, there is no hint as to what to put in the file!

When I go to run my tests from within Eclipse it errors out saying "failed to load Application Context"....of course.

Update:

Here is test-applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
    <description>Holds application context for testing of the domain module.</description>

    <!-- Imports the uuid generator bean definitions -->
    <import resource="resources/domain-uuid.xml"/>  
</beans>

My project directory is like this:

domain/
   src/
      main/
         java/
         resources/
      test/
         java/
         resources/ (location of test-applicationContext.xml)

Just for fun I also tried to build from the mvn command line via "mvn clean test" and I got the following errors which may be my real problem:

package org.springframework.test.context does not exist

package org.springframework.test.context.junit4 does not exist

cannot find symbol
symbol: class ContextConfiguration
@ContextConfiguration({"/resources/test-applicationContext.xml"})

cannot find symbol
symbol: class SpringJUnit4ClassRunner
@RunWith(SpringJUnit4ClassRunner.class)
+1  A: 

when you say the path is "/test-applicationContext.xml" it means at the root of the class path. where did u put the file?

daedlus
I put it under "src/test/resources", but other than the XML schema declarations and a single import -- its empty!
HDave
+2  A: 

What to put in the app context file. The way the TestContext Framework works is that it allows you to reuse app wiring in the context of your integration tests. So for the most part, there isn't anything special to tests you'd put inside your app context config files. If your controller has a service bean dependency in your app, then it will have that in your integration test too. If your DAO has a SessionFactory in your app, then same for your integration test. That way you don't have to wire all that stuff up all over again when you write integration tests. Very cool.

I said for the most part above because there's at least one exception that comes to mind. Normally your app will use JNDI to locate a DataSource, but in an integration test (at least an out-of-container integration test), you won't normally have a JNDI environment available. So you should typically isolate the DataSource bean creation to a separate file, and use a JNDI version for your live app and a non-JNDI version (e.g. just create a straight BasicDataSource, say) for your integration test. Here's an example of the former:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myStoreDS" resource-ref="true"/>

and here's an example of the latter:

<bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close"
    p:driverClassName="${dataSource.driverClassName}"
    p:url="${dataSource.url}"
    p:username="${dataSource.username}"
    p:password="${dataSource.password}" />

These would go in separate files. The first might go in beans-datasource.xml for normal app use and the second in beans-datasource-it.xml for integration tests. The configuration that's common to normal app use and integration tests (i.e., the vast majority of your bean config in most cases) should be in a common config file or files.

Also, Spring 3 introduces a new jdbc namespace that allows you to create an embedded database, like an HSQLDB database or a Derby database, etc. It looks like this:

<jdbc:embedded-database id="dataSource">
    <jdbc:script location="classpath:hsql/schema.sql" />
    <jdbc:script location="classpath:hsql/test-data.sql" />
</jdbc:embedded-database>

That would replace the BasicDataSource config described above, if you want to use this.

Why the error is happening. The error you are seeing is happening because your @ContextConfiguration value is implicitly indicating that the app context file should be on the classpath. IMPORTANT: Remove the /resources piece. That is Maven innards; when it builds your JAR or WAR, it copies the contents of the resources directory into your classpath, not resources itself. That should help.

EDIT:

To address the "no symbol found" errors, you will need to add your test dependencies to your Maven POM. This will be JUnit and the Spring Test module as well, both with <scope>test</scope>. In addition if you are using a mock framework like Mockito, you will need to add that dependency (with test scope) to your POM as well. Try that and please report back on what happens.

Willie Wheeler
Sorry -- I'm not using JNDI or DAOs. I just don't know where to create the textContext! You say "app context file" -- what is that?
HDave
The "app context file" is just your Spring configuration XML file. The one that has a top-level <beans> element. You can put it directly under WEB-INF, or you can put it on the classpath, like under src/test/resource like you said in one of your other comments. It might help for you to add your directory structure (the relevant parts) to your original post and also show what your test-applicationContext.xml file contains.
Willie Wheeler
OK -- I think I get it. I don't have to specify TestContext anywhere. Spring just "knows" somehow (how???). The problem is that Spring can't find my configuration file.
HDave
Correct, Spring will take care of the TestContext part. The developer shouldn't normally have to mess around with the TestContext at all. It is just what the test framework uses to manage contextual information about the test case (as implemented by your test class) and the test currently being run (as implemented by your test method). So if you have the @DirtiesContext annotation on a test method, for instance, then the framework would mark the application context (that is, your bean container) as being dirty so it can be reloaded after running the test.
Willie Wheeler
I just added an edit to this comment; see above.
Willie Wheeler
+2  A: 

Hello,

To find it directly under src/test/resources, change it to:

@ContextConfiguration({"classpath:/test-applicationContext.xml"})

When you're not specifying anything, then Spring search in the same package as the test class.

Espen