Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
与dbunitspringframeworkjava的集成测试_Java_Spring - Fatal编程技术网

与dbunitspringframeworkjava的集成测试

与dbunitspringframeworkjava的集成测试,java,spring,Java,Spring,我正在尝试使用DBUnit和SpringFramework编写一个集成测试。我已经复制了我写的代码,但是内存中的数据库连接有问题。我还复制了堆栈跟踪 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration @Transactional @TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesCon

我正在尝试使用DBUnit和SpringFramework编写一个集成测试。我已经复制了我写的代码,但是内存中的数据库连接有问题。我还复制了堆栈跟踪

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

        private MockMvc mockMvc;

        @Before
        public void setUp() {
            mockMvc = MockMvcBuilders.xmlConfigSetup("applicationContext.xml").build();
        }

        @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("id", 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())));
        }
    }
login.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <person id="1" email="qwerty@asdf.com" username="qwerty"
        hostname="asdf.com" pass="password1234" />

    <person id="2" email="zxcvb@asdf.com" username="zxcvb"
        hostname="asdf.com" pass="password1234" />
</dataset>

如果您已经配置了
transactionManager
dataSource
,那么问题很可能是在您指定的位置找不到配置文件

如果它位于类路径中,则应将
类路径:
前缀放在配置位置字符串中

mockMvc = MockMvcBuilders.xmlConfigSetup("classpath:applicationContext.xml").build();
mockMvc = MockMvcBuilders.xmlConfigSetup("classpath:applicationContext.xml").build();