From a7276eb191eabaa52138388366c5e28c1e1f1584 Mon Sep 17 00:00:00 2001 From: Sjmillington Date: Sun, 21 Oct 2018 17:42:54 +0100 Subject: [PATCH 1/2] [BAEL-2256] Guide to simple date format unit tests --- .../SimpleDateFormatUnitTest.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java b/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java new file mode 100644 index 0000000000..ee994247a4 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung; + +import org.junit.Test; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; +import java.util.logging.Logger; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + + +public class SimpleDateFormatUnitTest { + + private static final Logger logger = Logger.getLogger(SimpleDateFormatUnitTest.class.getName()); + + @Test + public void givenSpecificDate_whenFormatted_checkFormatCorrect() throws Exception { + SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); + assertEquals("24-05-1977", formatter.format(new Date(233345223232L))); + } + + @Test + public void givenSpecificDate_whenFormattedUsingDateFormat_checkFormatCorrect() throws Exception { + DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT); + assertEquals("5/24/77", formatter.format(new Date(233345223232L))); + } + + @Test + public void givenStringDate_whenParsed_checkDateCorrect() throws Exception{ + SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); + Date myDate = new Date(233276400000L); + Date parsedDate = formatter.parse("24-05-1977"); + assertEquals(myDate.getTime(), parsedDate.getTime()); + } + + @Test + public void givenFranceLocale_whenFormatted_checkFormatCorrect() throws Exception{ + SimpleDateFormat franceDateFormatter = new SimpleDateFormat("EEEEE dd-MMMMMMM-yyyy", Locale.FRANCE); + Date myWednesday = new Date(1539341312904L); + assertTrue(franceDateFormatter.format(myWednesday).startsWith("vendredi")); + } + + @Test + public void given2TimeZones_whenFormatted_checkTimeDifference() throws Exception { + Date now = new Date(); + + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE dd-MMM-yy HH:mm:ssZ"); + + simpleDateFormat.setTimeZone(TimeZone.getTimeZone("Europe/London")); + logger.info(simpleDateFormat.format(now)); + //change the date format + simpleDateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York")); + logger.info(simpleDateFormat.format(now)); + } +} From 06be85c4a183549f2bc6a6e99f35276014e0dafb Mon Sep 17 00:00:00 2001 From: Sam Millington Date: Sun, 21 Oct 2018 18:06:39 +0100 Subject: [PATCH 2/2] Added 'thens' to conform with given_when_then format --- .../simpledateformat/SimpleDateFormatUnitTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java b/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java index ee994247a4..ba4ea32e21 100644 --- a/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java +++ b/core-java/src/test/java/com/baeldung/simpledateformat/SimpleDateFormatUnitTest.java @@ -18,19 +18,19 @@ public class SimpleDateFormatUnitTest { private static final Logger logger = Logger.getLogger(SimpleDateFormatUnitTest.class.getName()); @Test - public void givenSpecificDate_whenFormatted_checkFormatCorrect() throws Exception { + public void givenSpecificDate_whenFormatted_thenCheckFormatCorrect() throws Exception { SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); assertEquals("24-05-1977", formatter.format(new Date(233345223232L))); } @Test - public void givenSpecificDate_whenFormattedUsingDateFormat_checkFormatCorrect() throws Exception { + public void givenSpecificDate_whenFormattedUsingDateFormat_thenCheckFormatCorrect() throws Exception { DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT); assertEquals("5/24/77", formatter.format(new Date(233345223232L))); } @Test - public void givenStringDate_whenParsed_checkDateCorrect() throws Exception{ + public void givenStringDate_whenParsed_thenCheckDateCorrect() throws Exception{ SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); Date myDate = new Date(233276400000L); Date parsedDate = formatter.parse("24-05-1977"); @@ -38,14 +38,14 @@ public class SimpleDateFormatUnitTest { } @Test - public void givenFranceLocale_whenFormatted_checkFormatCorrect() throws Exception{ + public void givenFranceLocale_whenFormatted_thenCheckFormatCorrect() throws Exception{ SimpleDateFormat franceDateFormatter = new SimpleDateFormat("EEEEE dd-MMMMMMM-yyyy", Locale.FRANCE); Date myWednesday = new Date(1539341312904L); assertTrue(franceDateFormatter.format(myWednesday).startsWith("vendredi")); } @Test - public void given2TimeZones_whenFormatted_checkTimeDifference() throws Exception { + public void given2TimeZones_whenFormatted_thenCheckTimeDifference() throws Exception { Date now = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEEE dd-MMM-yy HH:mm:ssZ");