Skip to content

Commit

Permalink
Fix duplicated test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
stijn-dejongh committed Sep 22, 2024
1 parent 4868314 commit 3a8c391
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod

private String replaceCapitals(String name) {
name = name.replaceAll("([A-Z])", " $1").toLowerCase();
name = name.replaceAll("([0-9]+)", " $1");
name = name.replaceAll("(\\d+)", " $1");
name = name.replace("given", "GIVEN");
name = name.replace("when", "WHEN");
name = name.replace("then", "THEN");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* <p>
Expand Down Expand Up @@ -181,7 +180,7 @@ private void validate() {
.apply(this.data)
.apply(errorTemplate.failure(this.data)))
.map(FailureBuilder::build)
.collect(Collectors.toList());
.toList();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
class FallibleTest {

public final List<String> NAUGHTY_WORDS = singletonList("string");
public final String NAUGHTY_ERROR = "Your word seems to contain a naughty bit [%s]";
public static final String NAUGHTY_ERROR = "Your word seems to contain a naughty bit [%s]";

static class ConsumerServiceStub<T> {

Expand Down Expand Up @@ -110,23 +110,23 @@ void givenAConditionWithAnErrorCode_whenContitionIsNotMet_thenTheFailureContains
List<Failure> errors = Fallible
.of("StringToCheck")
.ensure(FallibleTest.this::mayNotContainNaughtyWords,
d -> b -> b.errorCode("NAUGHTY_ERROR")
d -> b -> b.errorCode(NAUGHTY_ERROR)
)
.ensure(StringUtils::isNotBlank,
d -> b -> b.errorCode("MUST_BE_FILLED_IN")
)
.failures();

assertThat(errors).hasSize(1);
assertThat(errors).extracting(Failure::getErrorCode).containsExactly("NAUGHTY_ERROR");
assertThat(errors).extracting(Failure::getErrorCode).containsExactly(NAUGHTY_ERROR);
}

@Test
void givenMultipleConditionsWithErrorCodes_whenContitionsAreNotMet_thenTheFailureContainsAllCodes() {
List<Failure> errors = Fallible
.of("")
.ensure(FallibleTest.this::mayNotContainNaughtyWords,
d -> b -> b.errorCode("NAUGHTY_ERROR")
d -> b -> b.errorCode(NAUGHTY_ERROR)
)
.ensure(StringUtils::isNotBlank,
d -> b -> b.errorCode("MUST_BE_FILLED_IN")
Expand Down Expand Up @@ -175,23 +175,23 @@ void givenAConditionWithAnErrorCode_whenContitionIsNotMet_thenTheFailureContains
.<String>data()
.compliesTo(
FallibleTest.this::mayNotContainNaughtyWords)
.elseFail(withCode("NAUGHTY_ERROR")))
.elseFail(withCode(NAUGHTY_ERROR)))
.ensure(FieldValidationRule
.<String>data()
.compliesTo(StringUtils::isNotBlank)
.elseFail(withCode("MUST_BE_FILLED_IN")))
.failures();

assertThat(errors).hasSize(1);
assertThat(errors).extracting(Failure::getErrorCode).containsExactly("NAUGHTY_ERROR");
assertThat(errors).extracting(Failure::getErrorCode).containsExactly(NAUGHTY_ERROR);
}
}

@Nested
@DisplayName("Fallible conditional menthod executions when failure")
class FallibleFailedConditionalTests {

public final Condition<ConsumerServiceStub<String>> NOT_YET_CALLED = new Condition<>(
public static final Condition<ConsumerServiceStub<String>> NOT_YET_CALLED = new Condition<>(
c -> !c.isCalled(), "Precondition: Service is not yet called");
private ConsumerServiceStub<String> stringConsumer;

Expand Down Expand Up @@ -241,7 +241,7 @@ void givenAFailureActionIsDefined_andMultipleConditionsAreChained_whenTheFirstCo
@DisplayName("Fallible conditional menthod executions when succesful")
class FallibleConditionalExecutionsTests {

public final Condition<ConsumerServiceStub<String>> NOT_YET_CALLED = new Condition<>(
public static final Condition<ConsumerServiceStub<String>> NOT_YET_CALLED = new Condition<>(
c -> !c.isCalled(), "Precondition: Service is not yet called");
private ConsumerServiceStub<String> stringConsumer;

Expand Down Expand Up @@ -381,24 +381,6 @@ void givenThatNoTemplateIsProvided_andAReasonIsProvided_whenAFailureOccurs_theRe
assertThat(failures).extracting(Failure::getReason).contains("Input can not be blank");
}

@Test
void givenATemplateWithoutDataReference_whenAFailureOccurs_thenAFailureExistsWithADerivativeMessage() {

List<Failure> failures = Fallible
.of("")
.errorTemplate(template(
s -> "The string [" + s + "] does not match rule: {%s}"))
.ensure(StringUtils::isNotBlank,
s -> b -> b.reason("Input can not be blank")
)
.failures();

assertThat(failures).hasSize(1);
assertThat(failures)
.extracting(Failure::getReason)
.contains("The string [] does not match rule: {Input can not be blank}");
}

}

Boolean mayNotContainNaughtyWords(String toCheck) {
Expand Down

0 comments on commit 3a8c391

Please sign in to comment.