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.

ControllerTest

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:LoginControllerTest2-context.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseSetup("login.xml")
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;

    @Resource
    private WebApplicationContext wac;

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

    @Before
    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        

Answers


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

Search




Leave an answer