From 6e95419389fb388b6eba3b52a0ead837d7e4dc12 Mon Sep 17 00:00:00 2001 From: Alexey Zinchenko Date: Sun, 26 May 2019 01:17:24 +0300 Subject: [PATCH] Added unit tests for formatter classes. --- .../CustomDateTimeFormatterUnitTest.java | 43 +++++++++++++++++++ .../OrderStatusFormatterUnitTest.java | 40 +++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 src/test/java/com/github/prominence/carrepair/formatter/CustomDateTimeFormatterUnitTest.java create mode 100644 src/test/java/com/github/prominence/carrepair/formatter/OrderStatusFormatterUnitTest.java diff --git a/src/test/java/com/github/prominence/carrepair/formatter/CustomDateTimeFormatterUnitTest.java b/src/test/java/com/github/prominence/carrepair/formatter/CustomDateTimeFormatterUnitTest.java new file mode 100644 index 0000000..9b2b650 --- /dev/null +++ b/src/test/java/com/github/prominence/carrepair/formatter/CustomDateTimeFormatterUnitTest.java @@ -0,0 +1,43 @@ +package com.github.prominence.carrepair.formatter; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.junit4.SpringRunner; + +import java.time.LocalDateTime; +import java.time.format.DateTimeParseException; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +@RunWith(SpringRunner.class) +public class CustomDateTimeFormatterUnitTest { + + private CustomDateTimeFormatter customDateTimeFormatter = new CustomDateTimeFormatter(); + + @Test + public void whenParseNull_thenReturnNull() { + assertThat(customDateTimeFormatter.parse(null, null)).isNull(); + } + + @Test + public void whenPrintNull_thenReturnNUll() { + assertThat(customDateTimeFormatter.print(null, null)).isNull(); + } + + @Test + public void whenParseCorrectValue_thenReturnLocalDateTime() { + LocalDateTime expectedLocalDateTime = LocalDateTime.of(1996, 2, 7, 14, 12, 12); + assertThat(customDateTimeFormatter.parse("1996-02-07 14:12:12", null)).isEqualTo(expectedLocalDateTime); + } + + @Test(expected = DateTimeParseException.class) + public void whenParseIncorrectValue_thenThrowAnException() { + // expected exception + customDateTimeFormatter.parse("qwerty", null); + } + + @Test + public void whenPrintCorrectValue_thenReturnCorrectString() { + assertThat(customDateTimeFormatter.print(LocalDateTime.of(1996, 2, 7, 14, 12, 12), null)).isEqualTo("1996-02-07 14:12:12"); + } +} diff --git a/src/test/java/com/github/prominence/carrepair/formatter/OrderStatusFormatterUnitTest.java b/src/test/java/com/github/prominence/carrepair/formatter/OrderStatusFormatterUnitTest.java new file mode 100644 index 0000000..4f2f70f --- /dev/null +++ b/src/test/java/com/github/prominence/carrepair/formatter/OrderStatusFormatterUnitTest.java @@ -0,0 +1,40 @@ +package com.github.prominence.carrepair.formatter; + +import com.github.prominence.carrepair.enums.OrderStatus; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +@RunWith(SpringRunner.class) +public class OrderStatusFormatterUnitTest { + + private OrderStatusFormatter orderStatusFormatter = new OrderStatusFormatter(); + + @Test + public void whenParseNull_thenReturnNull() { + assertThat(orderStatusFormatter.parse(null, null)).isNull(); + } + + @Test + public void whenPrintNull_thenReturnNUll() { + assertThat(orderStatusFormatter.print(null, null)).isNull(); + } + + @Test + public void whenParseCorrectValue_thenReturnOrderStatus() { + assertThat(orderStatusFormatter.parse("Done", null)).isEqualTo(OrderStatus.DONE); + } + + @Test(expected = IllegalArgumentException.class) + public void whenParseIncorrectValue_thenThrowAnException() { + // expected exception + orderStatusFormatter.parse("Incorrect", null); + } + + @Test + public void whenPrintCorrectValue_thenReturnCorrectString() { + assertThat(orderStatusFormatter.print(OrderStatus.SCHEDULED, null)).isEqualTo("Scheduled"); + } +}