junit.framework.ComparisonFailure: table count exp

2019-08-10 14:04发布

问题:

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