0
\$\begingroup\$

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

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! I hope you get good answers. \$\endgroup\$ – Phrancis Apr 23 '18 at 20:50
2
\$\begingroup\$

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, so ExpectedResult should be expectedResult and str_time2 should be strTime2.
  • Maybe it's necessary, but I'm sceptical of the need for the conversion from Date to DateTime.
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.