gpt4 book ai didi

java - junit.framework.ComparisonFailure : table count expected [1] but was [95]

转载 作者:行者123 更新时间:2023-11-29 03:02:55 26 4
gpt4 key购买 nike

我是 spring 和 dbunit 的新手,在测试我的 dao 层时遇到问题。该条目已成功插入,但测试以失败告终。你能帮帮我吗?

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;


import com.epam.lab.marharytakhramtsova.javalab.task1.dao.TagDAO;
import com.epam.lab.marharytakhramtsova.javalab.task1.dto.Tag;
import com.github.springtestdbunit.DbUnitTestExecutionListener;
import com.github.springtestdbunit.annotation.DatabaseSetup;
import com.github.springtestdbunit.annotation.ExpectedDatabase;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/spring-config.xml" })
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
public class TestTagDAO {

@Autowired
private TagDAO tagDAO;

@Test
@DatabaseSetup(value = "/TagDAOTest.xml")
@ExpectedDatabase(value= "/expectedData.xml")
public void testInsert() throws Exception {
Tag tagToInsert = new Tag(4, "insertedTag");
tagDAO.insert(tagToInsert);
}

}

TagDAOTest.xml

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<tag tag_id = "3" tag_name="myTag3" />
</dataset>

expectedData.xml

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<tag tag_id = "3" tag_name="myTag3" />
<tag tag_id = "4" tag_name="insertedTag" />
</dataset>

这是故障痕迹

enter image description here

非常感谢您的帮助!

最佳答案

试试这个

@Test
@DatabaseSetup(value = "/TagDAOTest.xml")
@ExpectedDatabase(assertionMode=DatabaseAssertionMode.NON_STRICT, value= "/expectedData.xml")
public void testInsert() throws Exception {
Tag tagToInsert = new Tag(4, "insertedTag");
tagDAO.insert(tagToInsert);
}

关于java - junit.framework.ComparisonFailure : table count expected [1] but was [95],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33730837/

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