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