\$\begingroup\$
\$\endgroup\$
1
I am carrying out unit testing for my project but i am not sure i am doing it correctly. The example below calculates the difference between 2 time duration's.
public void diffTime2() throws Exception {
//Calculsates the difference
String ExpectedResult = "1:0:0";
String str_time1 = "04 13, 2018 09:00:00";
String str_time2 = "04 13, 2018 08:00:00";
DateFormat simpleDateFormat3 = new SimpleDateFormat("MM dd, yyyy HH:mm:ss");
Date date1 = simpleDateFormat3.parse(str_time1);
Date date2 = simpleDateFormat3.parse(str_time2);
DateTime dt1 = new DateTime(date2);
DateTime dt2 = new DateTime(date1);
long hours = (Hours.hoursBetween(dt1, dt2).getHours() % 24);
long mins = (Minutes.minutesBetween(dt1, dt2).getMinutes() % 60);
long secs = (Seconds.secondsBetween(dt1, dt2).getSeconds() % 60);
String result = (hours+":"+mins+":"+secs);
//System.out.println(hours+":"+mins+":"+secs);
assertEquals(ExpectedResult, result);
}
It successfully runs and seems to be OKbut i am not sure that i am completing the unit testing exactly how i should.
This is the result that is returned
Any observation about this is greatly appreciated
Phrancis
20.5k6 gold badges69 silver badges155 bronze badges
-
1\$\begingroup\$ Welcome to Code Review! I hope you get good answers. \$\endgroup\$Phrancis– Phrancis2018年04月23日 20:50:43 +00:00Commented Apr 23, 2018 at 20:50
1 Answer 1
\$\begingroup\$
\$\endgroup\$
Some notes:
- This is not a unit test of your code. A unit test must at some point involve some part of your code, and it looks like this test uses only classes which exist in the Java SDK. The idea is not to check an idea for an implementation, but instead to detect bugs in your actual implementation.
- By convention instance variables use
camelCase
in Java, soExpectedResult
should beexpectedResult
andstr_time2
should bestrTime2
. - Maybe it's necessary, but I'm sceptical of the need for the conversion from
Date
toDateTime
.
lang-java