Skip to content

Commit 2a1af9b

Browse files
committedFeb 27, 2020
Avoid unhandled exception in validateTestTarget
`InteractionRunner.validateTestTarget()` throws an `IndexOutOfBoundsException` when it tries to `return annotatedFields[0]` if the collection is empty. Instead it should return nothing as it adds an `Exception` to the `errors` list prior to the return statement, and this fulfills the expectation of the caller and mirrors the other `validateX()` methods in the class.
1 parent 7e81778 commit 2a1af9b

File tree

1 file changed

+1
-3
lines changed
  • provider/pact-jvm-provider-junit/src/main/kotlin/au/com/dius/pact/provider/junit

1 file changed

+1
-3
lines changed
 

‎provider/pact-jvm-provider-junit/src/main/kotlin/au/com/dius/pact/provider/junit/InteractionRunner.kt

+1-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import org.junit.runner.Description
2626
import org.junit.runner.Runner
2727
import org.junit.runner.notification.Failure
2828
import org.junit.runner.notification.RunNotifier
29-
import org.junit.runners.model.FrameworkField
3029
import org.junit.runners.model.FrameworkMethod
3130
import org.junit.runners.model.InitializationError
3231
import org.junit.runners.model.Statement
@@ -117,15 +116,14 @@ open class InteractionRunner<I>(
117116

118117
protected fun hasOneConstructor() = testClass.javaClass.constructors.size == 1
119118

120-
protected fun validateTestTarget(errors: MutableList<Throwable>): FrameworkField {
119+
protected fun validateTestTarget(errors: MutableList<Throwable>) {
121120
val annotatedFields = testClass.getAnnotatedFields(TestTarget::class.java)
122121
if (annotatedFields.size != 1) {
123122
errors.add(Exception("Test class should have exactly one field annotated with ${TestTarget::class.java.name}"))
124123
} else if (!Target::class.java.isAssignableFrom(annotatedFields[0].type)) {
125124
errors.add(Exception("Field annotated with ${TestTarget::class.java.name} should implement " +
126125
"${Target::class.java.name} interface"))
127126
}
128-
return annotatedFields[0]
129127
}
130128

131129
protected fun validateRules(errors: List<Throwable>) {

0 commit comments

Comments
 (0)
Please sign in to comment.