Skip to content

Instantly share code, notes, and snippets.

@mike-rogers
Created May 3, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-rogers/2a238278a67e51627970 to your computer and use it in GitHub Desktop.
Save mike-rogers/2a238278a67e51627970 to your computer and use it in GitHub Desktop.
TDD in Java using JUnit Theories
@DataPoints
public static String[] usernames() {
return new String[] {
"user1", "user2", "user3", "user4", "user5", "", null
};
}
@Theory
public void shouldDenyAccessToUsersWhenExceptionsAreUnspecified(String username) {
FeatureFilter featureFilter = FeatureFilter
.describeFeature()
.addFeatureRef("myFeature",
thatIsAvailable()
.atEndpoint("/myEndpoint")
.andDisable());
assumeThat("Username should not be null nor empty", username, not(isEmptyOrNullString()));
assertThat(featureFilter.isAllowed("/myEndpoint", username, "myFeature"), is(false));
}
@Theory
public void shouldAllowAccessToUserWhenExceptionIsSpecified(String username) {
FeatureFilter featureFilter = FeatureFilter
.describeFeature()
.addFeatureRef("myFeature",
thatIsAvailable()
.atEndpoint("/myEndpoint")
.andDisable()
.exceptForUser(username));
assumeThat("Username should not be null nor empty", username, not(isEmptyOrNullString()));
assertThat(featureFilter.isAllowed("/myEndpoint", username, "myFeature"), is(true));
}
@Theory
public void shouldDisallowAccessToInvalidUserWhenExceptionIsSpecified(String validUser, String invalidUser) {
FeatureFilter featureFilter = FeatureFilter
.describeFeature()
.addFeatureRef("myFeature",
thatIsAvailable()
.atEndpoint("/myEndpoint")
.andDisable()
.exceptForUser(validUser));
assumeThat("Username should not be null nor empty", validUser, not(isEmptyOrNullString()));
assumeThat("Username should not be null nor empty", invalidUser, not(isEmptyOrNullString()));
assumeThat("Users should be different", validUser, not(equalTo(invalidUser)));
assertThat(featureFilter.isAllowed("/myEndpoint", invalidUser, "myFeature"), is(false));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment