Writing Tests for ISO8601Date.java class of the Open Event Android App
ISO8601Date.java class of Open Event Android App was a util class written to perform the date manipulation functions and ensure the code base got more simpler and deterministic. However it was equally important to test the result from this util class so as to ensure the result returned by it was what we wanted. A test class named “DateTest.java” was written to ensure all the edge cases of conversion of the dates string from one timezone to another timezone were handled properly.
For writing unit tests, first we needed to add these libraries as dependencies in the app’s top level build.gradle file as shown below:
dependencies { testCompile 'junit:junit:4.12' }
Then a JUnit 4 Test class, which was a Java class containing the required test methods was created. The structure of the class looked like this:
public class DateTest { @Test public void methodName() { } }
Next step was including all the required methods which ensured the util class returned the correct results according to our needs. Various edge cases were taken into account by including functions like converting of date string from local time zone to specified timezone, from international timezone to local timezone, from local timezone to international timezone and many more. Some of the methods which were added in the class are shown below:
- Test of conversion from local timezone to specified timezone:
This function aimed at ensuring the util class worked well with date conversion from local time zone to a specified timezone. An example as shown below was taken where the conversion of the date string was tested from UTC timezone to Singapore timezone.
@Test public void shouldConvertLocalTimeZoneDateStringToSpecifiedTimeZoneDateString() { ISO8601Date.setTimeZone("UTC"); ISO8601Date.setEventTimeZone("Asia/Singapore"); String dateString1 = "2017-06-02T07:59:10Z"; String actualString = ISO8601Date.getTimeZoneDateStringFromString(dateString1); String expectedString = "Thu, 01 Jun 2017, 23:59, UTC"; Assert.assertEquals(expectedString, actualString); }
- Test of conversion from local timezone to international timezone:
This function aimed at ensuring the util class worked well with the date conversion from local timezone to international timezone. An example as shown below was taken where the conversion of the date string was tested from Amsterdam timezone to Singapore timezone.
@Test public void shouldConvertLocalTimeZoneDateStringToInternationalTimeZoneDateString() { ISO8601Date.setTimeZone("Europe/Amsterdam"); ISO8601Date.setEventTimeZone("Asia/Singapore"); String dateString = "2017-06-02T02:29:10Z"; String actualString = ISO8601Date.getTimeZoneDateStringFromString(dateString); String expectedString = "Thu, 01 Jun 2017, 20:29, GMT+02:00"; Assert.assertEquals(expectedString, actualString); }
Above were some functions added to ensure the conversion of a date string from one timezone to another was correct and thus ensured the util class was working properly and returned the results as required.
The last thing left was running the test to check the results the util class returned. For this we had to do two things:
- Sync the project with Gradle.
- Run the test by right clicking on the class and selecting “Run” option.
Through this we were able to run the test and check the output of the util class on different cases through the results which could be seen on the Android Monitor in the Android Studio.
Related Links:
- This link is about building effective unit tests in android. (https://developer.android.com/training/testing/unit-testing/index.html)
- This link is about the unit testing on date processing. (https://stackoverflow.com/questions/565289/unit-testing-code-that-does-date-processing-based-on-todays-date)
You must be logged in to post a comment.