-
Notifications
You must be signed in to change notification settings - Fork 745
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove exemption for tests from EmptyCatch
The style guide is being updated in unknown commit. Using `assertThrows` style assertions is now preferred over using try/catch statements for expected exception tests. Startblock: * unknown commit is submitted PiperOrigin-RevId: 674409794
- Loading branch information
Showing
5 changed files
with
45 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,8 @@ | |
|
||
package com.google.errorprone.bugpatterns; | ||
|
||
import static org.junit.Assert.fail; | ||
|
||
import java.io.FileNotFoundException; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author [email protected] (Ding Yuan) | ||
|
@@ -126,13 +124,4 @@ public void catchIsLoggedOnly() { | |
System.out.println("Caught an exception: " + t); | ||
} | ||
} | ||
|
||
@Test | ||
public void expectedException() { | ||
try { | ||
System.err.println(); | ||
fail(); | ||
} catch (Exception expected) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,10 @@ | |
|
||
package com.google.errorprone.bugpatterns; | ||
|
||
import static org.junit.Assert.fail; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* @author [email protected] (Ding Yuan) | ||
*/ | ||
|
@@ -32,4 +36,14 @@ public void catchIsCompleteEmpty() { | |
|
||
} | ||
} | ||
|
||
@Test | ||
public void expectedException() { | ||
try { | ||
System.err.println(); | ||
fail(); | ||
// BUG: Diagnostic contains: | ||
} catch (Exception expected) { | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
The [Google Java Style Guide §6.2][style] states: | ||
|
||
> It is very rarely correct to do nothing in response to a caught exception. | ||
> (Typical responses are to log it, or if it is considered "impossible", rethrow | ||
> it as an AssertionError.) | ||
> | ||
> When it truly is appropriate to take no action whatsoever in a catch block, | ||
> the reason this is justified is explained in a comment. | ||
When writing tests that expect an exception to be thrown, prefer using | ||
[`Assert.assertThrows`][assertthrows] instead of writing a try-catch. That is, | ||
prefer this: | ||
|
||
```java | ||
assertThrows(NoSuchElementException.class, () -> emptyStack.pop()); | ||
``` | ||
|
||
instead of this: | ||
|
||
```java | ||
try { | ||
emptyStack.pop(); | ||
fail(); | ||
} catch (NoSuchElementException expected) { | ||
} | ||
``` | ||
|
||
[style]: https://google.github.io/styleguide/javaguide.html#s6.2-caught-exceptions | ||
|
||
[assertthrows]: https://junit.org/junit4/javadoc/latest/org/junit/Assert.html#assertThrows(java.lang.Class,%20org.junit.function.ThrowingRunnable) |