Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ErrorCollector#checkSucceeds() generic #1013

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/junit/rules/ErrorCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public Object call() throws Exception {
* Execution continues, but the test will fail at the end if
* {@code callable} threw an exception.
*/
public Object checkSucceeds(Callable<Object> callable) {
public <T> T checkSucceeds(Callable<? extends T> callable) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should really be:

public <T> T checkSucceeds(Callable<T> callable) {

.. because of the return type of Callable.call(). Stated another way, if you pass in a Callable<Integer> then the return type should be a Integer, but the signature you have would mean "return some super type of Integer"

try {
return callable.call();
} catch (Throwable e) {
addError(e);
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,17 @@ public Object call() throws Exception {
throw new RuntimeException("first!");
}
});
collector.checkSucceeds(new Callable<Object>() {
public Object call() throws Exception {
collector.checkSucceeds(new Callable<Integer>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also an example that doesn't throw an exception and uses a `Callable' (where "something" isn't 'Object') and verify that the return value is the same as the passed-in value?

public Integer call() throws Exception {
throw new RuntimeException("second!");
}
});
Integer result = collector.checkSucceeds(new Callable<Integer>() {
public Integer call() throws Exception {
return 1;
}
});
assertEquals(Integer.valueOf(1), result);
}
}

Expand Down