Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与dbunitio异常Java的集成测试_Java_Ios_Spring - Fatal编程技术网

与dbunitio异常Java的集成测试

与dbunitio异常Java的集成测试,java,ios,spring,Java,Ios,Spring,我在SpringFramework控制器上编写了一个集成测试,但在数据库方面存在一些问题。我不明白为什么dbunit找不到我的schema.sql文件。我尝试使用绝对路径,但它也不起作用 控制器测试 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContext

我在SpringFramework控制器上编写了一个集成测试,但在数据库方面存在一些问题。我不明白为什么dbunit找不到我的schema.sql文件。我尝试使用绝对路径,但它也不起作用

控制器测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseSetup(value = "login.xml")
public class LoginControllerTest {

    private MockMvc mockMvc;
    private static final String JDBC_DRIVER = org.h2.Driver.class.getName();
    private static final String JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
    private static final String SCHEMA_PATH = "schema.sql";
    private static final String USER = "sa";
    private static final String PASSWORD = "";

    @BeforeClass
    public static void createSchema() throws Exception {
        RunScript.execute(JDBC_URL, USER, PASSWORD, SCHEMA_PATH, null, false);
    }

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.xmlConfigSetup("loginControllerTest-context.xml").build();
        IDataSet dataSet = readDataSet();
        cleanlyInsert(dataSet);
    }

    private IDataSet readDataSet() throws Exception {
        return new FlatXmlDataSetBuilder().build(new File("login.xml"));
    }

    private void cleanlyInsert(IDataSet dataSet) throws Exception {
        IDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
        databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
        databaseTester.setDataSet(dataSet);
        databaseTester.onSetup();
    }

    @Test
    @ExpectedDatabase("login.xml")
    public void testShowForm() throws Exception {
        mockMvc.perform(get("/login")).andExpect(status().isOk()).andExpect(view().name("/login"))
                .andExpect(forwardedUrl("/WebContent/j/login.jsp"))
                .andExpect(model().attribute("form", hasProperty("od", nullValue())))
                .andExpect(model().attribute("form", hasProperty("email", isEmptyOrNullString())))
                .andExpect(model().attribute("form", hasProperty("username", isEmptyOrNullString())))
                .andExpect(model().attribute("form", hasProperty("hostname", isEmptyOrNullString())))
                .andExpect(model().attribute("form", hasProperty("pass", isEmptyOrNullString())));
    }
}
<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:component-scan base-package="com.profiles.controller.test" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.jdbcx.JdbcDataSource" />
        <property name="url" value="jdbc:hsqldb:mem:login" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>
</beans>
loginControllerTest上下文

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DatabaseSetup(value = "login.xml")
public class LoginControllerTest {

    private MockMvc mockMvc;
    private static final String JDBC_DRIVER = org.h2.Driver.class.getName();
    private static final String JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
    private static final String SCHEMA_PATH = "schema.sql";
    private static final String USER = "sa";
    private static final String PASSWORD = "";

    @BeforeClass
    public static void createSchema() throws Exception {
        RunScript.execute(JDBC_URL, USER, PASSWORD, SCHEMA_PATH, null, false);
    }

    @Before
    public void setUp() throws Exception {
        mockMvc = MockMvcBuilders.xmlConfigSetup("loginControllerTest-context.xml").build();
        IDataSet dataSet = readDataSet();
        cleanlyInsert(dataSet);
    }

    private IDataSet readDataSet() throws Exception {
        return new FlatXmlDataSetBuilder().build(new File("login.xml"));
    }

    private void cleanlyInsert(IDataSet dataSet) throws Exception {
        IDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
        databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
        databaseTester.setDataSet(dataSet);
        databaseTester.onSetup();
    }

    @Test
    @ExpectedDatabase("login.xml")
    public void testShowForm() throws Exception {
        mockMvc.perform(get("/login")).andExpect(status().isOk()).andExpect(view().name("/login"))
                .andExpect(forwardedUrl("/WebContent/j/login.jsp"))
                .andExpect(model().attribute("form", hasProperty("od", nullValue())))
                .andExpect(model().attribute("form", hasProperty("email", isEmptyOrNullString())))
                .andExpect(model().attribute("form", hasProperty("username", isEmptyOrNullString())))
                .andExpect(model().attribute("form", hasProperty("hostname", isEmptyOrNullString())))
                .andExpect(model().attribute("form", hasProperty("pass", isEmptyOrNullString())));
    }
}
<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <tx:annotation-driven transaction-manager="transactionManager" />
    <context:component-scan base-package="com.profiles.controller.test" />

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.h2.jdbcx.JdbcDataSource" />
        <property name="url" value="jdbc:hsqldb:mem:login" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>
</beans>

堆栈跟踪

org.h2.message.DbException: IO Exception: "java.io.FileNotFoundException: schema.sql (The system cannot find the file specified)"; "schema.sql" [90031-191]
    at org.h2.message.DbException.get(DbException.java:168)
    at org.h2.message.DbException.convertIOException(DbException.java:330)
    at org.h2.tools.RunScript.process(RunScript.java:333)
    at org.h2.tools.RunScript.execute(RunScript.java:303)
    at com.profiles.controller.test.LoginControllerTest.createSchema(LoginControllerTest.java:54)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:49)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36)
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.h2.jdbc.JdbcSQLException: IO Exception: "java.io.FileNotFoundException: schema.sql (The system cannot find the file specified)"; "schema.sql" [90031-191]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
    ... 18 more
Caused by: java.io.FileNotFoundException: schema.sql (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at org.h2.store.fs.FilePathDisk.newInputStream(FilePathDisk.java:321)
    at org.h2.store.fs.FileUtils.newInputStream(FileUtils.java:218)
    at org.h2.tools.RunScript.process(RunScript.java:185)
    at org.h2.tools.RunScript.process(RunScript.java:328)
    ... 15 more
org.h2.message.DbException:IO异常:“java.IO.FileNotFoundException:schema.sql(系统找不到指定的文件)”;“schema.sql”[90031-191]
位于org.h2.message.DbException.get(DbException.java:168)
位于org.h2.message.DbException.convertIOException(DbException.java:330)
位于org.h2.tools.RunScript.process(RunScript.java:333)
位于org.h2.tools.RunScript.execute(RunScript.java:303)
在com.profiles.controller.test.LoginControllerTest.createSchema(LoginControllerTest.java:54)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:497)
位于org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:49)
位于org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:36)
位于org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
位于org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
位于org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
位于org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
原因:org.h2.jdbc.JdbcSQLException:IO异常:“java.IO.FileNotFoundException:schema.sql(系统找不到指定的文件)”;“schema.sql”[90031-191]
位于org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
... 还有18个
原因:java.io.FileNotFoundException:schema.sql(系统找不到指定的文件)
位于java.io.FileInputStream.open0(本机方法)
在java.io.FileInputStream.open(FileInputStream.java:195)
位于java.io.FileInputStream。(FileInputStream.java:138)
位于java.io.FileInputStream。(FileInputStream.java:93)
位于org.h2.store.fs.FilePathDisk.newInputStream(FilePathDisk.java:321)
位于org.h2.store.fs.FileUtils.newInputStream(FileUtils.java:218)
位于org.h2.tools.RunScript.process(RunScript.java:185)
位于org.h2.tools.RunScript.process(RunScript.java:328)
... 还有15个
解决方案


当我将sql文件移动到测试源文件夹下的文件夹并将路径设置为-classpath:/sql/schema.sql

文件在文件系统中的位置?schema.sql文件在哪里?它在
resources
目录下吗?@Jens文件位于LoginControllerTest@user2004685是否必须将其移动到资源目录中?请尝试
this.getClass().getResource(“schema.sql”).toExternalForm()文件在文件系统中的位置?schema.sql文件在哪里?它在
resources
目录下吗?@Jens文件位于LoginControllerTest@user2004685是否必须将其移动到资源目录中?请尝试
this.getClass().getResource(“schema.sql”).toExternalForm()