Commit 66044174 authored by Thomas Briner's avatar Thomas Briner
Browse files

add exception handling example

parent c3d7cda7
package com.community.hsr.testing.weekenddiscount;
import com.community.hsr.testing.util.DateFactory;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
class WeekendDiscountValidatorExceptionHandlingExampleTest {
WeekendDiscountValidator weekendDiscountValidator = new WeekendDiscountValidator();
@Test
void isInWeekend() throws ValidatorNotYetInitializedException {
weekendDiscountValidator.initializeWithWeekendNumber(4);
boolean authorizedForDiscount = weekendDiscountValidator.isAuthorizedForDiscount(DateFactory.createDate(2019, 6, 23, 0, 0, 0));
Assertions.assertThat(authorizedForDiscount).isTrue();
}
@Tag("negative")
@Test
void throwsExceptionIfNotInitialized() {
org.junit.jupiter.api.Assertions.assertThrows(
ValidatorNotYetInitializedException.class,
() -> weekendDiscountValidator.isAuthorizedForDiscount(DateFactory.createDate(2019, 1, 1, 0, 0, 0)));
}
@Tag("negative")
@Test
void throwsExceptionMessageIsCorrectIfNotInitialized() {
Throwable t = null;
try {
weekendDiscountValidator.isAuthorizedForDiscount(DateFactory.createDate(2019, 1, 1, 0, 0, 0));
} catch (ValidatorNotYetInitializedException e) {
t = e;
}
Assertions.assertThat(t).isNotNull();
Assertions.assertThat(t.getLocalizedMessage()).contains("not initialized");
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment