Skip to content

Commit

Permalink
expectThrows was renamed to assertThrows
Browse files Browse the repository at this point in the history
context:
junit-team/junit4@627b85a

Fixes #942

RELNOTES: N/A

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=186266107
  • Loading branch information
cushon authored and ronshapiro committed Feb 20, 2018
1 parent 38ff1bc commit ebef066
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.expectThrows;

import com.google.errorprone.BugPattern.Category;
import com.google.errorprone.BugPattern.LinkType;
Expand Down Expand Up @@ -197,7 +196,7 @@ final class BugPatternTestClass {}

BugPattern annotation = BugPatternTestClass.class.getAnnotation(BugPattern.class);
ValidationException e =
expectThrows(ValidationException.class, () -> BugPatternValidator.validate(annotation));
assertThrows(ValidationException.class, () -> BugPatternValidator.validate(annotation));
assertThat(e.getMessage()).contains("Name must not contain whitespace");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.expectThrows;

import com.google.common.collect.ImmutableMap;
import com.google.errorprone.ErrorProneOptions.Severity;
Expand Down Expand Up @@ -57,7 +56,7 @@ public void malformedOptionThrowsProperException() throws Exception {
badArgs.forEach(
arg -> {
InvalidCommandLineOptionException expected =
expectThrows(
assertThrows(
InvalidCommandLineOptionException.class,
() -> ErrorProneOptions.processArgs(Arrays.asList(arg)));
assertThat(expected.getMessage()).contains("invalid flag");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package com.google.errorprone.apply;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.expectThrows;

import com.google.common.collect.ImmutableList;
import com.sun.source.tree.TreeVisitor;
Expand Down Expand Up @@ -518,7 +518,7 @@ public OrganizedImports organizeImports(List<Import> imports) {
});

imports.add("import java.util.List");
IllegalStateException exception = expectThrows(IllegalStateException.class, imports::toString);
IllegalStateException exception = assertThrows(IllegalStateException.class, imports::toString);
assertEquals(
"Expected 1 import(s) in the organized imports but it contained 0", exception.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,12 @@ public Fix build(List<? extends StatementTree> throwingStatements) {
return baseFix;
}
SuggestedFix.Builder fix = SuggestedFix.builder().merge(baseFix);
fix.addStaticImport("org.junit.Assert.assertThrows");
StringBuilder fixPrefix = new StringBuilder();
if (newAsserts.isEmpty()) {
fix.addStaticImport("org.junit.Assert.assertThrows");
fixPrefix.append("assertThrows");
} else {
fix.addStaticImport("org.junit.Assert.expectThrows");
fixPrefix.append(String.format("%s thrown = expectThrows", exceptionClass));
if (!newAsserts.isEmpty()) {
fixPrefix.append(String.format("%s thrown = ", exceptionClass));
}
fixPrefix.append("assertThrows");
fixPrefix.append(String.format("(%s.class, () -> ", exceptionClass));
boolean useExpressionLambda =
throwingStatements.size() == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState

// Provide a fix for one of the classic blunders:
// rewrite `try { ..., fail(); } catch (AssertionError e) { ... }`
// to `AssertionError e = expectThrows(AssertionError.class, () -> ...); ...`.
// to `AssertionError e = assertThrows(AssertionError.class, () -> ...); ...`.
private static Optional<Fix> buildFix(
JCTry tryStatement, MethodInvocationTree tree, VisitorState state) {
if (!ASTHelpers.getSymbol(tree).getSimpleName().contentEquals("fail")) {
Expand Down Expand Up @@ -132,13 +132,13 @@ private static Optional<Fix> buildFix(
state.getSourceForNode(catchTree.getParameter().getType())))
.replace(endPosition, state.getEndPosition(catchTree), (expression ? "" : "}") + ");\n");
} else {
fix.addStaticImport("org.junit.Assert.expectThrows")
fix.addStaticImport("org.junit.Assert.assertThrows")
.prefixWith(tryStatement, state.getSourceForNode(catchTree.getParameter()))
.replace(
tryStatement.getStartPosition(),
startPosition,
String.format(
" = expectThrows(%s.class, () -> ",
" = assertThrows(%s.class, () -> ",
state.getSourceForNode(catchTree.getParameter().getType())))
.replace(
/* startPos= */ endPosition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,11 @@ public void refactoring() throws IOException {
"out/Test.java", //
"import static com.google.common.truth.Truth.assertThat;",
"import static org.junit.Assert.assertThrows;",
"import static org.junit.Assert.expectThrows;",
"import java.io.IOException;",
"import org.junit.Assert;",
"class Test {",
" void f() {",
" AssertionError t = expectThrows(AssertionError.class, () -> ",
" AssertionError t = assertThrows(AssertionError.class, () -> ",
" System.err.println());",
" assertThat(t).isInstanceOf(AssertionError.class);",
" assertThrows(AssertionError.class, () -> ",
Expand Down Expand Up @@ -193,12 +192,11 @@ public void refactoringStatements() throws IOException {
"out/Test.java", //
"import static com.google.common.truth.Truth.assertThat;",
"import static org.junit.Assert.assertThrows;",
"import static org.junit.Assert.expectThrows;",
"import java.io.IOException;",
"import org.junit.Assert;",
"class Test {",
" void f() {",
" AssertionError t = expectThrows(AssertionError.class, () -> {",
" AssertionError t = assertThrows(AssertionError.class, () -> {",
" System.err.println();",
" System.err.println();",
" });",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void expect() throws IOException {
"out/ExceptionTest.java",
"import static com.google.common.truth.Truth.assertThat;",
"import static org.hamcrest.MatcherAssert.assertThat;",
"import static org.junit.Assert.expectThrows;",
"import static org.junit.Assert.assertThrows;",
"",
"import java.io.IOException;",
"import java.nio.file.*;",
Expand All @@ -74,7 +74,7 @@ public void expect() throws IOException {
" if (true) {",
" Path p = Paths.get(\"NOSUCH\");",
" IOException thrown =",
" expectThrows(IOException.class, () -> Files.readAllBytes(p));",
" assertThrows(IOException.class, () -> Files.readAllBytes(p));",
" assertThat(thrown,",
" CoreMatchers.is(CoreMatchers.instanceOf(IOException.class)));",
" assertThat(thrown.getCause(),",
Expand Down Expand Up @@ -117,7 +117,7 @@ public void noExceptionType() throws IOException {
"out/ExceptionTest.java",
"import static com.google.common.truth.Truth.assertThat;",
"import static org.hamcrest.MatcherAssert.assertThat;",
"import static org.junit.Assert.expectThrows;",
"import static org.junit.Assert.assertThrows;",
"",
"import java.io.IOException;",
"import java.nio.file.*;",
Expand All @@ -131,7 +131,7 @@ public void noExceptionType() throws IOException {
" public void test() throws Exception {",
" Path p = Paths.get(\"NOSUCH\");",
" Files.readAllBytes(p);",
" Throwable thrown = expectThrows(Throwable.class, () -> Files.readAllBytes(p));",
" Throwable thrown = assertThrows(Throwable.class, () -> Files.readAllBytes(p));",
" assertThat(thrown, CoreMatchers.is(CoreMatchers.instanceOf(IOException.class)));",
" }",
"}")
Expand Down Expand Up @@ -255,7 +255,7 @@ public void isA_hasCauseThat() throws IOException {
.addOutputLines(
"out/ExceptionTest.java",
"import static com.google.common.truth.Truth.assertThat;",
"import static org.junit.Assert.expectThrows;",
"import static org.junit.Assert.assertThrows;",
"",
"import java.io.IOException;",
"import java.nio.file.*;",
Expand All @@ -269,7 +269,7 @@ public void isA_hasCauseThat() throws IOException {
" public void test() throws Exception {",
" Path p = Paths.get(\"NOSUCH\");",
" IOException thrown =",
" expectThrows(IOException.class, () -> Files.readAllBytes(p));",
" assertThrows(IOException.class, () -> Files.readAllBytes(p));",
" assertThat(thrown).hasCauseThat().isInstanceOf(IOException.class);",
" assertThat(thrown).hasCauseThat().isInstanceOf(IOException.class);",
" assertThat(Files.exists(p)).isFalse();",
Expand Down Expand Up @@ -308,7 +308,7 @@ public void typedMatcher() throws IOException {
"out/ExceptionTest.java",
"import static com.google.common.truth.Truth.assertThat;",
"import static org.hamcrest.MatcherAssert.assertThat;",
"import static org.junit.Assert.expectThrows;",
"import static org.junit.Assert.assertThrows;",
"",
"import java.io.IOException;",
"import java.nio.file.*;",
Expand All @@ -323,7 +323,7 @@ public void typedMatcher() throws IOException {
" public void test() throws Exception {",
" Path p = Paths.get(\"NOSUCH\");",
" IOException thrown =",
" expectThrows(IOException.class, () -> Files.readAllBytes(p));",
" assertThrows(IOException.class, () -> Files.readAllBytes(p));",
" assertThat(thrown, matcher);",
" assertThat(Files.exists(p)).isFalse();",
" }",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ public void containsExpectAsIdentifier_shouldBeTest() {
compilationHelper
.addSourceLines(
"Test.java",
"import static org.junit.Assert.expectThrows;",
"import static org.junit.Assert.assertThrows;",
"import org.junit.runner.RunWith;",
"import org.junit.runners.JUnit4;",
"@RunWith(JUnit4.class)",
"public class Test {",
" // BUG: Diagnostic contains: @Test",
" public void shouldDoSomething() {",
" expectThrows(null, null);",
" assertThrows(null, null);",
" }",
"}")
.doTest();
Expand All @@ -223,7 +223,7 @@ public void containsQualifiedExpect_shouldBeTest() {
"public class Test {",
" // BUG: Diagnostic contains: @Test",
" public void shouldDoSomething() {",
" Assert.expectThrows(null, null);",
" Assert.assertThrows(null, null);",
" }",
"}")
.doTest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.scanner.BuiltInCheckerSuppliers.getSuppliers;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.expectThrows;
import static org.junit.Assert.assertThrows;

import com.google.common.base.Joiner;
import com.google.common.base.Predicates;
Expand Down Expand Up @@ -161,7 +161,7 @@ public void plusDisallowsDuplicates() throws Exception {
ScannerSupplier ss2 = ScannerSupplier.fromBugCheckerClasses(OtherArrayEquals.class);

IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> ss1.plus(ss2));
assertThrows(IllegalArgumentException.class, () -> ss1.plus(ss2));
assertThat(thrown)
.hasMessageThat()
.contains(
Expand Down Expand Up @@ -198,7 +198,7 @@ public void plusDisallowsDifferentSeverities() throws Exception {
ScannerSupplier ss2 = ScannerSupplier.fromBugCheckerClasses(class2);

IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> ss1.plus(ss2));
assertThrows(IllegalArgumentException.class, () -> ss1.plus(ss2));

assertThat(class1).isNotEqualTo(class2);
assertThat(thrown)
Expand Down Expand Up @@ -468,7 +468,7 @@ public void applyOverridesThrowsExceptionWhenDisablingNonDisablableCheck() throw
ErrorProneOptions.processArgs(ImmutableList.of("-Xep:ArrayEquals:OFF"));

InvalidCommandLineOptionException exception =
expectThrows(InvalidCommandLineOptionException.class, () -> ss.applyOverrides(epOptions));
assertThrows(InvalidCommandLineOptionException.class, () -> ss.applyOverrides(epOptions));
assertThat(exception.getMessage()).contains("may not be disabled");
}

Expand All @@ -481,7 +481,7 @@ public void applyOverridesThrowsExceptionWhenDemotingNonDisablableCheck() throws
ErrorProneOptions.processArgs(ImmutableList.of("-Xep:ArrayEquals:WARN"));

InvalidCommandLineOptionException exception =
expectThrows(InvalidCommandLineOptionException.class, () -> ss.applyOverrides(epOptions));
assertThrows(InvalidCommandLineOptionException.class, () -> ss.applyOverrides(epOptions));
assertThat(exception.getMessage()).contains("may not be demoted to a warning");
}

Expand Down Expand Up @@ -613,7 +613,7 @@ public void disablingPackageLocation_unsuppressible() throws Exception {
ErrorProneOptions.processArgs(ImmutableList.of("-Xep:PackageLocation:OFF"));

InvalidCommandLineOptionException exception =
expectThrows(InvalidCommandLineOptionException.class, () -> ss.applyOverrides(epOptions));
assertThrows(InvalidCommandLineOptionException.class, () -> ss.applyOverrides(epOptions));
assertThat(exception.getMessage()).contains("may not be disabled");
}

Expand Down
6 changes: 3 additions & 3 deletions docs/bugpattern/AssertionFailureIgnored.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ try {
}
```

To avoid this issue, prefer JUnit's `assertThrows()` or `expectThrows()` API:
To avoid this issue, prefer JUnit's `assertThrows()` API:

```java
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.expectThrows;
import static org.junit.Assert.assertThrows;

@Test
public void testFailsWithAssertionError() {
AssertionError thrown = expectThrows(
AssertionError thrown = assertThrows(
AssertionError.class,
() -> {
doSomething();
Expand Down
6 changes: 3 additions & 3 deletions docs/bugpattern/ExpectedExceptionChecker.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public void testRemoveFails() {
}
```

To avoid this issue, prefer JUnit's `assertThrows()` or `expectThrows()` API:
To avoid this issue, prefer JUnit's `assertThrows()` API:

```java
import static org.junit.Assert.expectThrows;
import static org.junit.Assert.assertThrows;

@Test
public void testRemoveFails() {
AppendOnlyList list = new AppendOnlyList();
list.add(0, "a");
UnsupportedOperationException thrown = expectThrows(
UnsupportedOperationException thrown = assertThrows(
UnsupportedOperationException.class,
() -> {
list.remove(0);
Expand Down

0 comments on commit ebef066

Please sign in to comment.