0

.I have a project that has a spring-config.xml file in src/main/webapp/WEB-INF and an applicationContext.xml file in src/test/resources. I also have an abstract test base class for my unit tests in src/test/java looks something like:

@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
public abstract class AbstractTestBase extends AbstractTransactionalJUnit4SpringContextTests {
     //Common code and fields
}

All my unit tests extends this AbstractTestBase which points to the context within the src/test/resources or should. The problem arises when running my unit tests it is pulling in the spring-config.xml file.

There are other projects my team is working on that have the same file structure, same app context setup, and run as intended, but even when I have each file in the project side by side I don't see where their file runs and this one doesn't.

I am new to spring so I don't know what it is I should be looking for.

Are there any situations where Spring or Maven would not take the app context I'm handing it given all files exist? Is there anything I might be missing?

EDIT: corrected to reflect that one file is a spring-config file.

sparks
  • 736
  • 1
  • 9
  • 29
  • Maven should look for resource files in `src/test/resources`. It should never go into `src/main/webapp/WEB-INF` as it's not a Web application and doesn't even know about that location. What happens if your delete (move them) from that folder? – Sotirios Delimanolis Sep 05 '13 at 14:47
  • 1
    Are you sure you don't have multiple `applicationContext.xml` files on your classpath? Using that syntax will load the first one it finds and then stop, and the jar order isn't always predictable. – chrylis -cautiouslyoptimistic- Sep 05 '13 at 14:47
  • @SotiriosDelimanolis This does in fact look like a Web application, and it is customary to put Spring configuration in `src/main/webapp/WEB-INF`. – chrylis -cautiouslyoptimistic- Sep 05 '13 at 14:50
  • @chrylis It is a web application, but when running tests there shouldn't be any notion of a `WEB-INF`. – Sotirios Delimanolis Sep 05 '13 at 14:50
  • @SotiriosDelimanolis Ah, I just checked, and my `WEB-INF` context just does an `include` to one in `META-INF/spring`. It's been a while since I built an XML config, and I'm hoping to get away with annotation-only for my next couple of projects! – chrylis -cautiouslyoptimistic- Sep 05 '13 at 14:52
  • @SotiriosDelimanolis This is actually a server for a web application, I don't know if that matters – sparks Sep 05 '13 at 14:52
  • @chrylis the app context file under WEB-INF does have a different name and is the Spring config file, but it still is pulling that one. – sparks Sep 05 '13 at 14:53
  • 1
    @sparks Unless you're actually firing up the entire server as part of your test harness, it doesn't; the test configuration shouldn't need anything that's specified in `WEB-INF`, which should only be things like configuration for the `DispatcherServlet`. – chrylis -cautiouslyoptimistic- Sep 05 '13 at 14:53

1 Answers1

1

"classpath:/applicationContext.xml" should look under src/test/resources. But it should be noticed that using that syntax will load the first one it finds and then stop as mentioned by '@chrylis'.

I once had similar problem.

You must have been using an IDE. There must have been applicationContext.xml file in your target/test-classes/ (in Eclipse IDE) in your project directory that is a copy of your xml file under src/main/webapp/WEB-INF or xml file like it.

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
  • There is only one file named `applicationContext.xml` file under `src/test/resources` and there is a file of the same name as the spring config file (named `spring-config.xml`) that is under the `target/test-classes/` folder. 1) The name of the file is NOT `applicationContext.xml` 2) How do I change it to load the applicationContext.xml instead? – sparks Sep 05 '13 at 15:22
  • Not sure, but I would try deleting all the `test-classes/` and run the test again. Just trying. Also, I would recheck if I'm missing something else if I were you. – TheKojuEffect Sep 05 '13 at 15:54
  • Apparently trashing everything inside of test-classes/ seems to work. I have been able to move past this issue (into other unrelated ones) but it hasn't become an issue yet. – sparks Sep 05 '13 at 18:06