Added unit tests for formatter classes.

This commit is contained in:
Alexey Zinchenko 2019-05-26 01:17:24 +03:00
parent d52488a81b
commit 6e95419389
2 changed files with 83 additions and 0 deletions

View File

@ -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");
}
}

View File

@ -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");
}
}