为什么我在JUnit的返回列表中获得额外的方括号

时间:2018-04-18 10:49:29

标签: java list oop junit mockito

我有ff。测试代码:

@Before
    public void setup() {
        RefAccountType refAcctType = new RefAccountType();
        refAcctType.setCode("tax");
        refAcctType.setAccessLevel("1");
        refAcctType.setCreatedBy("anonymous");
        refAcctType.setCreatedDate(new Date(04/18/2018));
        refAcctType.setDescription("taxDesc");
        refAcctType.setEffectiveDate(new Date(04/18/2018));
        refAcctType.setExpiryDate(new Date(04/18/2019));
        refAcctType.setOrderSeq(new BigDecimal(0));
        refAcctType.setStatus("A");
        refAcctType.setUpdatedBy("anonymous1");
        refAcctType.setUpdatedDate(new Date(04/18/2018));
        refAcctType.setVersion("1");

        List<RefAccountType> refAcctTypeList = new ArrayList<>();
        refAcctTypeList.add(refAcctType);

        Mockito.when(refAccountTypeRepository.findAll())
        .thenReturn(refAcctTypeList);

    }

    @Test
    public void testFindAll() {
        List<RefAccountType> refAcctTypeList = new ArrayList<>();
        RefAccountType refAccountType = new RefAccountType( "tax","1", "anonymous", 
        new Date(04/18/2018), "taxDesc",new Date(04/18/2018),
        new Date(04/18/2019),new BigDecimal(0), "A", "anonymous1",  new Date(04/18/2018), "1");

        refAcctTypeList = refAccountTypeService.findAll();
        assertThat(refAcctTypeList).isEqualTo(refAccountType);
        }

但我不知道为什么它在运行测试时会返回错误。

org.junit.ComparisonFailure: expected:<[RefAccountType [code=tax, accessLevel=1, createdBy=anonymous, createdDate=Thu Jan 01 08:00:00 CST 1970, description=taxDesc, effectiveDate=Thu Jan 01 08:00:00 CST 1970, expiryDate=Thu Jan 01 08:00:00 CST 1970, orderSeq=0, status=A, updatedBy=anonymous1, updatedDate=Thu Jan 01 08:00:00 CST 1970, version=1]]> 


but was:<[[RefAccountType [code=tax, accessLevel=1, createdBy=anonymous, createdDate=Thu Jan 01 08:00:00 CST 1970, description=taxDesc, effectiveDate=Thu Jan 01 08:00:00 CST 1970, expiryDate=Thu Jan 01 08:00:00 CST 1970, orderSeq=0, status=A, updatedBy=anonymous1, updatedDate=Thu Jan 01 08:00:00 CST 1970, version=1]]]>

我怀疑错误是在额外的方括号上。 如何删除开头和结尾的额外括号,但结果是什么?

3 个答案:

答案 0 :(得分:0)

额外的方括号表示传递的引用是RefAccountType个对象的列表。当然,它不等于RefAccountType对象本身。

答案 1 :(得分:0)

好的,我刚刚将refAccountType添加到列表中,然后逐个断言数据。

List<RefAccountType> refAcctTypeList  = refAccountTypeService.findAll();
refAcctTypeList2.add(refAccountType);

assertThat(refAcctTypeList.get(0).getCode()).isEqualTo(refAcctTypeList2.get(0).getCode());
assertThat(refAcctTypeList.get(0).getAccessLevel()).isEqualTo(refAcctTypeList2.get(0).getAccessLevel());

答案 2 :(得分:0)

您正在将RefAccountType类型的对象与类型为List<RefAccountType>的列表进行比较。

您应该将RefAccountType objecs与:

进行比较
assertThat(refAcctTypeList).isNotNull(refAccountType);
assertThat(refAcctTypeList.get(0)).isEqualTo(refAccountType);

或类型List<RefAccountType>

assertThat(refAcctTypeList).isEqualTo(Arrays.asList(refAccountType));