Skip to content

Commit

Permalink
TestParameterValuesProvider: Allow provideValues(Context) to throw an…
Browse files Browse the repository at this point in the history
…y exception.

This avoids the need for a try-catch when calling testClass.getDeclaredConstructor().newInstance()

#44
  • Loading branch information
nymanjens committed Jan 17, 2024
1 parent 616afa4 commit 68c2a77
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ private static List<Object> getValuesFromProvider(
}
} 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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
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;

/**
Expand All @@ -36,7 +36,7 @@
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ private static List<Object> getValuesFromProvider(
}
} 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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
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;

/**
Expand All @@ -36,7 +36,7 @@
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() {
Expand Down

0 comments on commit 68c2a77

Please sign in to comment.