Skip to content

Commit

Permalink
Consider field name containing "." as fully qualified for @⁠FieldSource
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Apr 16, 2024
1 parent d253327 commit f188238
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static Field findField(Class<?> testClass, String fieldName) {
fieldName = fieldName.trim();

Class<?> clazz = testClass;
if (fieldName.contains("#")) {
if (fieldName.contains("#") || fieldName.contains(".")) {
String[] fieldParts = ReflectionUtils.parseFullyQualifiedFieldName(fieldName);
String className = fieldParts[0];
fieldName = fieldParts[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,9 @@ private static void assertStaticIsRequired(PreconditionViolationException except
"external @FieldSource fields must always be static.");
}

@Test
void throwsExceptionWhenFullyQualifiedFieldNameIsMissingFieldName() {
String fieldName = "org.example.wrongSyntax#"; // missing "fieldName"
@ParameterizedTest
@ValueSource(strings = { "org.example.MyUtils", "org.example.MyUtils#", "#fieldName" })
void throwsExceptionWhenFullyQualifiedFieldNameSyntaxIsInvalid(String fieldName) {
var exception = assertThrows(PreconditionViolationException.class,
() -> provideArguments(fieldName).toArray());

Expand All @@ -363,16 +363,6 @@ it must start with a fully qualified class name followed by a \
'#' and then the field name.""", fieldName, TestCase.class.getName());
}

@Test
void throwsExceptionWhenFullyQualifiedFieldNameIsMissingHashAndFieldName() {
String fieldName = "org.example.wrongSyntax"; // missing "#fieldName"
var exception = assertThrows(PreconditionViolationException.class,
() -> provideArguments(fieldName).toArray());

assertThat(exception.getMessage()).isEqualTo("Could not find field named [%s] in class [%s]", fieldName,
TestCase.class.getName());
}

@Test
void throwsExceptionWhenClassForExternalFieldCannotBeLoaded() {
var exception = assertThrows(JUnitException.class,
Expand All @@ -390,14 +380,15 @@ void throwsExceptionWhenLocalFieldDoesNotExist() {
TestCase.class.getName());
}

@Test
void throwsExceptionWhenExternalFieldDoesNotExist() {
@ParameterizedTest
@ValueSource(strings = { "nonExistentField", "strings()" })
void throwsExceptionWhenExternalFieldDoesNotExist(String fieldName) {
String factoryClass = ExternalFields.class.getName();

var exception = assertThrows(PreconditionViolationException.class,
() -> provideArguments(factoryClass + "#nonExistentField").toArray());
() -> provideArguments(factoryClass + "#" + fieldName).toArray());

assertThat(exception.getMessage()).isEqualTo("Could not find field named [nonExistentField] in class [%s]",
assertThat(exception.getMessage()).isEqualTo("Could not find field named [%s] in class [%s]", fieldName,
factoryClass);
}

Expand Down

0 comments on commit f188238

Please sign in to comment.