aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Nyman <jnyman@google.com>2024-01-17 10:25:48 +0000
committerJens Nyman <jnyman@google.com>2024-01-17 10:25:48 +0000
commit68c2a772720dbe8e926ab6b663566cb434769b98 (patch)
tree825825d4551a3e82be1af3428397e1f6317f08d4
parent616afa4d030631062a0f3c8854b44204f64a1d13 (diff)
downloadTestParameterInjector-68c2a772720dbe8e926ab6b663566cb434769b98.tar.gz
TestParameterValuesProvider: Allow provideValues(Context) to throw any exception.
This avoids the need for a try-catch when calling testClass.getDeclaredConstructor().newInstance() https://github.com/google/TestParameterInjector/issues/44
-rw-r--r--junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameter.java7
-rw-r--r--junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java4
-rw-r--r--junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameter.java7
-rw-r--r--junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java4
4 files changed, 18 insertions, 4 deletions
diff --git a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameter.java b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameter.java
index 6272e44..992c259 100644
--- a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameter.java
+++ b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameter.java
@@ -244,6 +244,13 @@ public @interface TestParameter {
}
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
+ } catch (Exception e) {
+ // Catch any unchecked exception that may come from `provideValues(Context)`
+ if (e instanceof RuntimeException) {
+ throw (RuntimeException) e;
+ } else {
+ throw new IllegalStateException(e);
+ }
}
}
}
diff --git a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java
index 4e62128..9fa8dc2 100644
--- a/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java
+++ b/junit4/src/main/java/com/google/testing/junit/testparameterinjector/TestParameterValuesProvider.java
@@ -17,13 +17,13 @@ package com.google.testing.junit.testparameterinjector;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.getOnlyElement;
-import java.util.NoSuchElementException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import java.lang.annotation.Annotation;
import java.util.List;
+import java.util.NoSuchElementException;
import javax.annotation.Nullable;
/**
@@ -36,7 +36,7 @@ import javax.annotation.Nullable;
public abstract class TestParameterValuesProvider
implements TestParameter.TestParameterValuesProvider {
- protected abstract List<?> provideValues(Context context);
+ protected abstract List<?> provideValues(Context context) throws Exception;
@Override
public final List<?> provideValues() {
diff --git a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameter.java b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameter.java
index 06a1cd5..40bd569 100644
--- a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameter.java
+++ b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameter.java
@@ -244,6 +244,13 @@ public @interface TestParameter {
}
} catch (ReflectiveOperationException e) {
throw new IllegalStateException(e);
+ } catch (Exception e) {
+ // Catch any unchecked exception that may come from `provideValues(Context)`
+ if (e instanceof RuntimeException) {
+ throw (RuntimeException) e;
+ } else {
+ throw new IllegalStateException(e);
+ }
}
}
}
diff --git a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java
index ac52755..2232d19 100644
--- a/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java
+++ b/junit5/src/main/java/com/google/testing/junit/testparameterinjector/junit5/TestParameterValuesProvider.java
@@ -17,13 +17,13 @@ package com.google.testing.junit.testparameterinjector.junit5;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Iterables.getOnlyElement;
-import java.util.NoSuchElementException;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import java.lang.annotation.Annotation;
import java.util.List;
+import java.util.NoSuchElementException;
import javax.annotation.Nullable;
/**
@@ -36,7 +36,7 @@ import javax.annotation.Nullable;
public abstract class TestParameterValuesProvider
implements TestParameter.TestParameterValuesProvider {
- protected abstract List<?> provideValues(Context context);
+ protected abstract List<?> provideValues(Context context) throws Exception;
@Override
public final List<?> provideValues() {