Home Ask Login Register

Developers Planet

Your answer is one click away!

RockOrDead February 2016

SpringFramework integration test Failed to load ApplicationContext

I write this integration test but now i do not know how to fix this IllegalStateException which is about that the application context can not be load. LoginControllerTest2-context.xml is in the same package with tested controller.


@ContextConfiguration(locations = { "classpath:LoginControllerTest2-context.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class })
public class LoginControllerTest2 {

    private static final String JDBC_DRIVER = "org.h2.Driver";
    private static final String JDBC_URL = "jdbc:h2:mem:login;DB_CLOSE_DELAY=-1";
    private static final String USER = "sa";
    private static final String PASS = "";
    private static final String SCHEMA_FILE = "h2.sql";
    private static final String DATASET = "login.xml";

    private MockMvc mockMvc;

    private WebApplicationContext wac;

    public static void createSchema() throws ClassNotFoundException {
        try {
            Connection conn = dataSource().getConnection();
            InputStreamReader in = new InputStreamReader(LoginControllerTest2.class.getResourceAsStream(SCHEMA_FILE));
            RunScript.execute(conn, in);
        } catch (Exception e) {
            // TODO: handle exception

    public void loadTestData() throws Exception {
        // mockMvc =
        // MockMvcBuilders.xmlConfigSetup("classpath:LoginControllerTest2-context.xml").build();

        mockMvc = MockMvcBuilders.webApplicationContextSetup(wac).build();

        // mockMvc =
        // MockMvcBuilders.annotationConfigSetup(LoginControllerTest2.class).build();

        IDataSet ids = n        


Lance Java February 2016

Somewhere in your code, you are attempting to lookup /test/loginController/h2.sql from the ServletContext.

I'm not familiar with MockMvc but I'd guess that you need to tell it where your test web root is so that ServletContext.getResource(...) can find the file.

Post Status

Asked in February 2016
Viewed 2,247 times
Voted 14
Answered 1 times


Leave an answer

Quote of the day: live life