gpt4 book ai didi

java - 与 DBUnit SpringFramework java 的集成测试

转载 作者:行者123 更新时间:2023-11-30 10:50:35 24 4
gpt4 key购买 nike

我正在尝试使用 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())));
}
}

登录.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)

最佳答案

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

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

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

关于java - 与 DBUnit SpringFramework java 的集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013570/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com