我正在尝试使用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="[email protected]" username="qwerty"
hostname="asdf.com" pass="password1234" />
<person id="2" email="[email protected]" 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)
最佳答案
如果已经配置了transactionManager
和dataSource
,则最有可能的问题是在给定的位置找不到配置文件。
如果它位于类路径中,则应将classpath:
前缀放在配置位置字符串中。
mockMvc = MockMvcBuilders.xmlConfigSetup("classpath:applicationContext.xml").build();