Reputation: 29
I have this Junit test that I am trying to write for a date. The piece of code the unit test is for is a function that returns a date. Below I have what I wrote for the code.
public void getLatestHistoricalLossDate() {
List<Overview> scenarioListTest3 = new ArrayList<Overview>();
String name = "third_scenario-test";
ISO8601DateFormat df = new ISO8601DateFormat();
Date estimationDate = null;
try {
estimationDate = df.parse("2017-01-28T22:25:51Z");
} catch (ParseException e) {
e.printStackTrace();
}
Date creationDate = null;
try {
creationDate = df.parse("2017-02-28T22:25:51Z");
} catch (ParseException e) {
e.printStackTrace();
}
Double balance = 131750000.0;
Double individualReviewImpairment = 1000.00;
Map<String, Double> baseline = new HashMap<String, Double>();
baseline.put("complete", 1000.0);
Map<String, Double> macroAdjustment = new HashMap<String, Double>();
macroAdjustment.put("complete", 2000.0);
Map<String, Double> qualitativeAdjustment = new HashMap<String, Double>();
qualitativeAdjustment.put("complete", 3000.0);
Date positionDate = null;
try {
positionDate = df.parse("2017-01-28T22:25:51Z");
} catch (ParseException e) {
e.printStackTrace();
}
Date lossHistoryDate = null;
try {
lossHistoryDate = df.parse("2017-01-28T22:25:51Z");
} catch (ParseException e) {
e.printStackTrace();
}
String status = "active";
Map<String, Integer> period = new HashMap<String, Integer>();
period.put("Q1", 2017);
boolean publish = true;
Overview ac = new Overview(4, name, estimationDate, creationDate, balance, individualReviewImpairment, baseline,
macroAdjustment, qualitativeAdjustment, positionDate, lossHistoryDate, status, period, publish);
scenarioListTest3.add(ac);
Mockito.when(scenarioDashboardService.getLatestHistoricalLossDate()).thenReturn(lossHistoryDate);
}
@Test
public void testgetLatestHistoricalLossDate() throws Exception {
getLatestHistoricalLossDate();
ISO8601DateFormat df = new ISO8601DateFormat();
Date testLossHistoryDate = scenarioDashboardService.getLatestHistoricalLossDate();
assertEquals(df.parse("2017-01-28T22:25:51Z"), testLossHistoryDate);
int expectedHistoricalDate = testLossHistoryDate.compareTo(testLossHistoryDate);
Date lossHistoricalDate = df.parse("2017-01-28T22:25:51Z");
assertEquals(expectedHistoricalDate, lossHistoricalDate);
}
When I run the test, it states that the expected result is 0 instead of the date listed which I don't understand why. Normally I would have the assertEquals()
statement as assertEquals(1, testLossHistoryDate.size());
but the .size
does not work with dates. Anyone know a different way to do the same thing?
Upvotes: 2
Views: 85
Reputation: 86296
assertNotNull(testLossHistoryDate);
This checks that there is exactly one date.
Since your methord’s return type is Date
, it cannot return more than one Date
. It can however return null
, that is, no date at all; but the above assertion will catch if that happens and fail the test.
PS The java.util.Date
is long outdated. You may do yourself the favour of looking into java.time
, the modern Java date and time API. It is so much nicer to work with. Link: Oracle Tutorial: Date Time.
Upvotes: 1