当前位置:  开发笔记 > 编程语言 > 正文

断言复杂对象时的Junit最佳实践

如何解决《断言复杂对象时的Junit最佳实践》经验,为你挑选了0个好方法。

我正在为遗留系统编写很多JUnit测试.

我经常会提出这样的问题:断言复杂对象的最佳方法是什么?

这是我目前的代码

public class SomeParserTest {

    @Test
    public void testParse() throws Exception {
        final SomeParser someParser = new SomeParser();
        someParser.parse("string from some file");

        final List listOfResults = someParser.getResults();
        assertThat(listOfResults, hasSize(5));

        assertResult(listOfResults.get(0), "20151223", 2411189L, isEmptyOrNullString(), "2.71", "16.99");
        assertResult(listOfResults.get(1), "20151229", 2411190L, isEmptyOrNullString(), "2.86", "17.9");
        assertResult(listOfResults.get(2), "20151229", 2411191L, is("1.26"), ".75", "23.95");
        assertResult(listOfResults.get(3), "20151229", 2411192L, is("2.52"), "1.5", "47.9");
        assertResult(listOfResults.get(4), "20151229", 2411193L, isEmptyOrNullString(), "2.71", "16.99");

        final List listofSubResuls = someParser.getSubResultOf(listOfResults.get(0));
        assertThat(listofSubResuls, hasSize(1));
        assertSubResult(listofSubResuls.get(0), 12.5D, "20151223", 1L, 14.87D, 16.99D, 0L, null, 67152L, "20151223", "2", 0L, "02411189", 56744349L);

        final List listofSubResuls1 = someParser.getListofBBBS(listOfResults.get(1));
        assertThat(listofSubResuls1, hasSize(2));
        assertSubResult(listofSubResuls1.get(0), 30.0D, "20151228", 1L, 12.53D, 17.9D, 0L, null, 67156L, "20151229", "2", 0L, "02411190", 56777888L);
        assertSubResult(listofSubResuls1.get(1), 33.3D, "20151228", 1L, 4.66D, 6.99D, 1L, "J", 67156L, "20151229", "2", 21L, "02411190", 56777889L);
//And 50 Lines more
    }

//  how to avoid so many parameters?
    private void assertSubResult(final SubResult subResult, final double someDouble, final String bestellDatum,
            final long someLong, final double someDouble2, final double someDouble3, final long someLong3,
            final String someString,
            final long someLong1,
            final String someString4, final String someString3, final long someLong4, final String rechnungsNummer,
            final long someLong2) {
        assertThat(subResult.getXXX(), is(nullValue()));
        assertThat(subResult.getXYX().getTag(), is(someDouble2));
        assertThat(subResult.getXYX(), is("some constant"));
//      and much more
    }

    //  how to avoid so many parameters?
    private void assertResult(final Result result, final String string1234, final long abc,
            final String string1, final String string12, final String string134) {
        assertThat(result.getXXX(), is(nullValue()));
        assertThat(result.getXYX().getTag(), is(someDouble2));
        assertThat(result.getXYX(), is("some constant"));
//      and much more
    }
}

没有简单的方法来测试这样一个解析器的每一步,因为它是遗留代码,所以我无法改变这一点.

谢谢你的帮助!

推荐阅读
拾味湖
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有