DbUnit检查自动生成ID

问题描述 投票:2回答:3

我需要使用DbUnit执行集成测试。我创建了数据集(测试前后),并使用@DatabaseSetup@ExpectedDatabase注释进行比较。在测试期间,创建了一个新的数据库行(它出现在测试数据集之后,我使用@ExpectedDatabase注释指定)。问题是行id是自动生成的(我正在使用Hibernate),因此行ID会永久更改。因此我的测试只传递一次,之后我需要在测试数据集之后更改id,但这不是我需要的。如果可以使用DbUnit解决此问题,您能否建议我解决此问题的任何解决方案。

spring hibernate dbunit
3个回答
2
投票

解决方案A:

使用分配的ID策略并使用单独的查询来检索业务逻辑中的下一个值。因此,您始终可以使用适当的数据库清理在持久性测试中分配已知的ID。请注意,这仅适用于使用Oracle Sequence的情况。

解决方案B:

如果我没有弄错,有一些方法类似于org.dbunit.Assertion中的assertEqualsIngoreColumns()。如果你不介意,你可以忽略id断言。通常我会通过对id的非空检查来弥补这一点。也许@ExpectedDatabase中有一些选项,但我不确定。

解决方案C:

我想知道是否有更好的解决方案,因为解决方案A引入了一些性能开销,而解决方案B牺牲了一点测试覆盖率。

你顺便使用什么版本的dbunit。我在2.4.9及以下版本中从未见过这些注释,它们看起来更容易使用。


0
投票

这个解决方法是保存我的皮肤到现在为止:

我实现了一个具有替换功能的AbstractDataSetLoader:

public class ReplacerDataSetLoader extends AbstractDataSetLoader {
    private Map<String, Object> replacements = new ConcurrentHashMap<>();

    @Override
    protected IDataSet createDataSet(Resource resource) throws Exception {
        FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
        builder.setColumnSensing(true);
        try (InputStream inputStream = resource.getInputStream()) {
            return createReplacementDataSet(builder.build(inputStream));
        }
    }

    /**
     * prepare some replacements
     * @param dataSet
     * @return
     */
    private ReplacementDataSet createReplacementDataSet(FlatXmlDataSet dataSet) {
        ReplacementDataSet replacementDataSet = new ReplacementDataSet(dataSet);

        //Configure the replacement dataset to replace '[null]' strings with null.
        replacementDataSet.addReplacementObject("[null]", null);
        replacementDataSet.addReplacementObject("[NULL]", null);
        replacementDataSet.addReplacementObject("[TODAY]", new Date());
        replacementDataSet.addReplacementObject("[NOW]", new Timestamp(System.currentTimeMillis()));

        for (java.util.Map.Entry<String, Object> entry : replacements.entrySet()) {
            replacementDataSet.addReplacementObject("["+entry.getKey()+"]", entry.getValue());
        }
        replacements.clear();

        return replacementDataSet;
    }

    public void replace(String replacement, Object value){
        replacements.put(replacement, value);
    }
}

通过这种方式,您可以以某种方式跟踪您需要的ID并替换您的睾丸

@DatabaseSetup(value="/test_data_user.xml")
@DbUnitConfiguration(dataSetLoaderBean = "replacerDataSetLoader")
public class ControllerITest extends WebAppConfigurationAware {

    //reference my test dbconnection so I can get last Id using regular query
    @Autowired
    DatabaseDataSourceConnection dbUnitDatabaseConnection;
    //reference my datasetloader so i can iteract with it
    @Autowired
    ColumnSensingFlatXMLDataSetLoader datasetLoader;


    private static Number lastid = Integer.valueOf(15156);
    @Before
    public void setup(){
        System.out.println("setting "+lastid);
        datasetLoader.replace("emp1", lastid.intValue()+1);
        datasetLoader.replace("emp2", lastid.intValue()+2);
    }

    @After
    public void tearDown() throws SQLException, DataSetException{
        ITable table = dbUnitDatabaseConnection.createQueryTable("ids", "select max(id) as id from company.entity_group");
        lastid = (Number)table.getValue(0, "id");
    }

    @Test
    @ExpectedDatabase(value="/expected_data.xml", assertionMode=DatabaseAssertionMode.NON_STRICT)
    public void test1() throws Exception{
        //run your  test logic
    }

    @Test
    @ExpectedDatabase(value="/expected_data.xml", assertionMode=DatabaseAssertionMode.NON_STRICT)
    public void test2() throws Exception{
        //run your  test logic
    }
}

我期望的数据集需要一些替换emp1和emp2

<?xml version='1.0' encoding='UTF-8'?>
<dataset>

    <company.entity_group ID="15155" corporate_name="comp1"/>
    <company.entity_group ID="15156" corporate_name="comp2"/>
    <company.entity_group ID="[emp1]" corporate_name="comp3"/>
    <company.entity_group ID="[emp2]" corporate_name="comp3"/>
    <company.ref_entity ID="1" entity_group_id="[emp1]"/>
    <company.ref_entity ID="2" entity_group_id="[emp2]"/>

</dataset>

0
投票

使用DatabaseAssertionMode.NO_STRICT,并从'expect.xml'中删除'id'列。 DBUnit将忽略此列。

© www.soinside.com 2019 - 2024. All rights reserved.