Failed to load ApplicationContext for JUnit test of Spring controller

As mentioned in the discussion: WEB-INF is not really part of the class path. If you use a common template, such as maven, use src/main/resources or src/test/resources to place the app-context.xml in. Then you can use classpath:.

Place your config file in src/main/resources/app-context.xml and use the following code:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:app-context.xml")
public class PersonControllerTest {
  ...
}

You can also create your test context with different configurations of beans.

Place your config file into src/test/resources/test-app-context.xml and use:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:test-app-context.xml")
public class PersonControllerTest {
  ...
}

Leave a Comment