-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added assertions for LocalDate #20
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2018 the original author or authors. | ||
*/ | ||
package org.assertj.jodatime.api; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertSame; | ||
|
||
import org.joda.time.LocalDate; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests for <code>{@link Assertions#assertThat(LocalDate)}</code>. | ||
* | ||
* @author Evgenii Strepetov | ||
*/ | ||
public class Assertions_assertThat_with_LocalDate_Test { | ||
|
||
@Test | ||
public void should_create_Assert() { | ||
LocalDateAssert assertions = Assertions.assertThat(new LocalDate()); | ||
assertNotNull(assertions); | ||
} | ||
|
||
@Test | ||
public void should_pass_actual() { | ||
LocalDate localDate = new LocalDate(); | ||
LocalDateAssert assertions = Assertions.assertThat(localDate); | ||
assertSame(localDate, assertions.getActual()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2018 the original author or authors. | ||
*/ | ||
package org.assertj.jodatime.api.localdate; | ||
|
||
import org.assertj.jodatime.api.JodaTimeBaseTest; | ||
import org.assertj.jodatime.api.LocalDateAssert; | ||
import org.joda.time.LocalDate; | ||
import org.junit.experimental.theories.DataPoint; | ||
|
||
import static org.junit.Assume.assumeTrue; | ||
|
||
/** | ||
* Base test class for {@link LocalDateAssert} tests. | ||
* | ||
* @author Evgenii Strepetov | ||
*/ | ||
public class LocalDateAssertBaseTest extends JodaTimeBaseTest { | ||
|
||
@DataPoint | ||
public static LocalDate localDate1 = new LocalDate(2000, 12, 14); | ||
@DataPoint | ||
public static LocalDate localDate2 = new LocalDate(1999, 12, 14); | ||
@DataPoint | ||
public static LocalDate localDate3 = new LocalDate(1999, 12, 15); | ||
@DataPoint | ||
public static LocalDate localDate4 = new LocalDate(1999, 11, 14); | ||
|
||
protected static void testAssumptions(LocalDate reference, LocalDate dateBefore, LocalDate dateAfter) { | ||
assumeTrue(dateBefore.isBefore(reference)); | ||
assumeTrue(dateAfter.isAfter(reference)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2018 the original author or authors. | ||
*/ | ||
package org.assertj.jodatime.api.localdate; | ||
|
||
import org.joda.time.LocalDate; | ||
import org.junit.Test; | ||
import org.junit.experimental.theories.Theories; | ||
import org.junit.experimental.theories.Theory; | ||
import org.junit.runner.RunWith; | ||
|
||
import static java.lang.String.format; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.util.FailureMessages.actualIsNull; | ||
import static org.assertj.jodatime.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Evgenii Strepetov | ||
*/ | ||
@RunWith(Theories.class) | ||
public class LocalDateAssert_isAfterOrEqualTo_Test extends LocalDateAssertBaseTest { | ||
|
||
@Theory | ||
public void test_isAfterOrEqual_assertion(LocalDate referenceDate, LocalDate dateBefore, LocalDate dateAfter) { | ||
// GIVEN | ||
testAssumptions(referenceDate, dateBefore, dateAfter); | ||
// WHEN | ||
assertThat(dateAfter).isAfterOrEqualTo(referenceDate); | ||
assertThat(dateAfter).isAfterOrEqualTo(referenceDate.toString()); | ||
assertThat(referenceDate).isAfterOrEqualTo(referenceDate); | ||
assertThat(referenceDate).isAfterOrEqualTo(referenceDate.toString()); | ||
// THEN | ||
verify_that_isAfterOrEqual_assertion_fails_and_throws_AssertionError(dateBefore, referenceDate); | ||
} | ||
|
||
@Test | ||
public void test_isAfterOrEqual_assertion_error_message() { | ||
assertThatThrownBy(() -> { | ||
LocalDate actualLocalDate = new LocalDate(2000, 1, 5); | ||
LocalDate localDateToCheck = new LocalDate(2012, 1, 1); | ||
assertThat(actualLocalDate).isAfterOrEqualTo(localDateToCheck); | ||
}).isInstanceOf(AssertionError.class) | ||
.hasMessage(format("%nExpecting:%n <2000-01-05>%nto be after or equals to:%n <2012-01-01>%n")); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_both_actual_and_parameter_are_null() { | ||
expectException(AssertionError.class, actualIsNull()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to use |
||
LocalDate actual = null; | ||
assertThat(actual).isAfterOrEqualTo((LocalDate) null); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_actual_is_null_and_parameter_is_not() { | ||
expectException(AssertionError.class, actualIsNull()); | ||
LocalDate actual = null; | ||
assertThat(actual).isAfterOrEqualTo(new LocalDate()); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_actual_is_null_and_localDate_as_string_parameter_is_not() { | ||
expectException(AssertionError.class, actualIsNull()); | ||
LocalDate actual = null; | ||
assertThat(actual).isAfterOrEqualTo(new LocalDate().toString()); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_localDate_parameter_is_null_but_actual_is_not() { | ||
expectException(IllegalArgumentException.class, "The LocalDate to compare actual with should not be null"); | ||
assertThat(new LocalDate()).isAfterOrEqualTo((LocalDate) null); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_localDate_as_string_parameter_is_null() { | ||
expectException(IllegalArgumentException.class, | ||
"The String representing the LocalDate to compare actual with should not be null"); | ||
assertThat(new LocalDate()).isAfterOrEqualTo((String) null); | ||
assertThat((LocalDate) null).isAfterOrEqualTo((String) null); | ||
} | ||
|
||
private static void verify_that_isAfterOrEqual_assertion_fails_and_throws_AssertionError(LocalDate dateToCheck, | ||
LocalDate reference) { | ||
assertThatThrownBy(() -> assertThat(dateToCheck).isAfterOrEqualTo(reference)) | ||
.isInstanceOf(AssertionError.class); | ||
|
||
assertThatThrownBy(() -> assertThat(dateToCheck).isAfterOrEqualTo(reference.toString())) | ||
.isInstanceOf(AssertionError.class); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2018 the original author or authors. | ||
*/ | ||
package org.assertj.jodatime.api.localdate; | ||
|
||
import org.joda.time.LocalDate; | ||
import org.junit.Test; | ||
import org.junit.experimental.theories.Theories; | ||
import org.junit.experimental.theories.Theory; | ||
import org.junit.runner.RunWith; | ||
|
||
import static java.lang.String.format; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.util.FailureMessages.actualIsNull; | ||
import static org.assertj.jodatime.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Evgenii Strepetov | ||
*/ | ||
@RunWith(Theories.class) | ||
public class LocalDateAssert_isAfter_Test extends LocalDateAssertBaseTest { | ||
|
||
@Theory | ||
public void test_isAfter_assertion(LocalDate referenceDate, LocalDate dateBefore, LocalDate dateAfter) { | ||
// GIVEN | ||
testAssumptions(referenceDate, dateBefore, dateAfter); | ||
// WHEN | ||
assertThat(dateAfter).isAfter(referenceDate); | ||
assertThat(dateAfter).isAfter(referenceDate.toString()); | ||
// THEN | ||
verify_that_isAfter_assertion_fails_and_throws_AssertionError(referenceDate, referenceDate); | ||
verify_that_isAfter_assertion_fails_and_throws_AssertionError(dateBefore, referenceDate); | ||
} | ||
|
||
@Test | ||
public void test_isAfter_assertion_error_message() { | ||
assertThatThrownBy(() -> { | ||
LocalDate actualLocalDate = new LocalDate(2000, 1, 5); | ||
LocalDate localDateToCheck = new LocalDate(2012, 1, 1); | ||
assertThat(actualLocalDate).isAfter(localDateToCheck); | ||
}).isInstanceOf(AssertionError.class) | ||
.hasMessage(format("%nExpecting:%n <2000-01-05>%nto be strictly after:%n <2012-01-01>%n")); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_actual_is_null() { | ||
LocalDate nullLocalDate = null; | ||
LocalDate other = new LocalDate(); | ||
|
||
assertThatThrownBy(() -> assertThat(nullLocalDate).isAfter(other)).as("other=%s", other) | ||
.hasMessage(actualIsNull()); | ||
assertThatThrownBy(() -> assertThat(nullLocalDate).isAfter(other.toString())).as("other=%s", other) | ||
.hasMessage(actualIsNull()); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_localDate_parameter_is_null() { | ||
expectException(IllegalArgumentException.class, "The LocalDate to compare actual with should not be null"); | ||
assertThat(new LocalDate()).isAfter((LocalDate) null); | ||
} | ||
|
||
@Test | ||
public void should_fail_if_localDate_as_string_parameter_is_null() { | ||
expectException(IllegalArgumentException.class, | ||
"The String representing the LocalDate to compare actual with should not be null"); | ||
assertThat(new LocalDate()).isAfter((String) null); | ||
} | ||
|
||
private static void verify_that_isAfter_assertion_fails_and_throws_AssertionError(LocalDate dateToCheck, | ||
LocalDate reference) { | ||
assertThatThrownBy(() -> assertThat(dateToCheck).isAfter(reference)) | ||
.isInstanceOf(AssertionError.class); | ||
|
||
assertThatThrownBy(() -> assertThat(dateToCheck).isAfter(reference.toString())) | ||
.isInstanceOf(AssertionError.class); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since assertj-joda-time relies on assertj-core 3.x, we can take advantage to
catchThrowable
, that will make the test easier to read and not having to usefailBecauseExpectedAssertionErrorWasNotThrown
.I'm keen on using a BBD style test (GIVEN, WHEN THEN).
This comment applies to any other test checking for an expected
AssertionError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored to assertThatThrownBy() everywhere it is applicable