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

孤街醉人 提交于 2020-01-05 18:10:38

问题


I'm new to spring and dbunit and have problem with testing my dao layer. The entry is inserted succsessfully, but test ends with failure. Could you help me, please?

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>

Here is failure trace

I would be very grateful for the help!


回答1:


Try this

@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);
}


来源:https://stackoverflow.com/questions/33730837/junit-framework-comparisonfailure-table-count-expected-1-but-was-95

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!