为什么我在 JUnit 中的退货列表中收到额外的方括号



我有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]]]>

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

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

好的,

我只是将 refAccountType 添加到列表中,然后 1 个断言数据。

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

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

您应该将RefAccountType对象与以下对象进行比较:

assertThat(refAcctTypeList).isNotNull(refAccountType);
assertThat(refAcctTypeList.get(0)).isEqualTo(refAccountType);

或带有类型 List<RefAccountType>

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

相关内容

  • 没有找到相关文章

最新更新