Search code examples
javalistjunitpositionjunit4

Confused About Junit Test for List<Position>


I need to write a test for a List and I can figure out how. I tried writing a test that was similar to my tests for List but obviously, since they are different and not the same, it didn't work. Here is what I have so far. Not sure where to go from here.

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

I wanted to get a date back from the test and then see if that date matches with the date I created for the test. I was going to write a String in the test but can't since my output is a date.


Solution

  • You could make ISO8601DateFormat df = new ISO8601DateFormat(); a property of your test class and then use it in both getPosition() method and in test method. And replace

    assertEquals("position_1", uploadDate);
    

    with

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

    You could even use a String constant for "2017-02-28T22:25:51Z" to se that constant in getPositions() and in test method.