对列表的 Junit 测试感到困惑<Position>

Confused About Junit Test for List<Position>

我需要为 List 编写测试,我知道怎么做。我尝试编写一个类似于我对 List 的测试的测试,但显然,由于它们不同且不相同,因此它没有用。这是我到目前为止所拥有的。不知道从这里去哪里。

    public void getPosition() {

    List<Positions> positionTest = new ArrayList<Positions>();

    String name = "position_1";
    ISO8601DateFormat df = new ISO8601DateFormat();
    Date asOfDate = null;
    try {
        asOfDate = df.parse("2017-01-28T22:25:51Z");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    Double balance = 131750000.0;
    Date uploadDate = null;
    try {
        uploadDate = df.parse("2017-02-28T22:25:51Z");
    } catch (ParseException e) {
        e.printStackTrace();
    }

    String username = "admin";
    String filename = "position.xlsx";
    Integer loanCount = 2889;

    Positions p = new Positions(name, asOfDate, balance, uploadDate, username, filename, loanCount);

    positionTest.add(p);
    Mockito.when(scenarioDashboardService.getPostionFileSummaryData());
}

@Test
public void testgetPostionFileSummaryData() {

    getPosition();

    List<Positions> testPositions = scenarioDashboardService.getPostionFileSummaryData();
    assertEquals(1, testPositions.size());

    Date uploadDate = testPositions.get(0).getUploadDate();
    assertEquals("position_1", uploadDate);
}

我想从测试中获取日期,然后查看该日期是否与我为测试创建的日期匹配。我打算在测试中写一个字符串,但不能,因为我的输出是一个日期。

您可以将 ISO8601DateFormat df = new ISO8601DateFormat(); 设为测试 class 的 属性,然后在 getPosition() 方法和测试方法中使用它。 并替换

assertEquals("position_1", uploadDate);

Date expectedDate = df.df.parse("2017-02-28T22:25:51Z");
assertEquals(expectedDate, uploadDate);

您甚至可以为 "2017-02-28T22:25:51Z" 使用 String 常量来在 getPositions() 和测试方法中使用该常量。