使用DBUnit SpringFramework java进行集成测试

时间:2016-01-26 12:11:05

标签: java spring

我正在尝试使用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>

堆栈跟踪

java.lang.IllegalStateException: Unable to find a DB Unit database connection, missing one the following beans: [dbUnitDatabaseConnection, dataSource]
    at com.github.springtestdbunit.DbUnitTestExecutionListener.getDatabaseConnectionUsingCommonBeanNames(DbUnitTestExecutionListener.java:111)
    at com.github.springtestdbunit.DbUnitTestExecutionListener.prepareTestInstance(DbUnitTestExecutionListener.java:94)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139)
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    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)

1 个答案:

答案 0 :(得分:1)

如果您已经配置了transactionManagerdataSource,则问题很可能是在您提供的位置找不到配置文件。

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

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