Skip to content

Commit

Permalink
Use try/execute/fail/catch instead of the strongly discouraged `@Te…
Browse files Browse the repository at this point in the history
…st(expected=...)`

cl/437399696
  • Loading branch information
kluever authored and ejona86 committed Mar 28, 2022
1 parent d3f7dc0 commit 31ce764
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
Expand Up @@ -84,15 +84,23 @@ public void leaf_insert() {
assertEquals(2, ret.size());
}

@Test(expected = AssertionError.class)
@Test
public void collisionLeaf_assertKeysDifferent() {
Key key1 = new Key(0);
new CollisionLeaf<>(key1, new Object(), key1, new Object());
try {
new CollisionLeaf<>(key1, new Object(), key1, new Object());
throw new Error();
} catch (AssertionError expected) {
}
}

@Test(expected = AssertionError.class)
@Test
public void collisionLeaf_assertHashesSame() {
new CollisionLeaf<>(new Key(0), new Object(), new Key(1), new Object());
try {
new CollisionLeaf<>(new Key(0), new Object(), new Key(1), new Object());
throw new Error();
} catch (AssertionError expected) {
}
}

@Test
Expand Down
9 changes: 7 additions & 2 deletions core/src/test/java/io/grpc/internal/AtomicBackoffTest.java
Expand Up @@ -17,6 +17,7 @@
package io.grpc.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.Test;
import org.junit.runner.RunWith;
Expand All @@ -25,9 +26,13 @@
/** Unit tests for {@link AtomicBackoff}. */
@RunWith(JUnit4.class)
public class AtomicBackoffTest {
@Test(expected = IllegalArgumentException.class)
@Test
public void mustBePositive() {
new AtomicBackoff("test", 0);
try {
new AtomicBackoff("test", 0);
fail();
} catch (IllegalArgumentException expected) {
}
}

@Test
Expand Down
Expand Up @@ -27,6 +27,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
Expand Down Expand Up @@ -486,10 +487,14 @@ public void subchannelStateIsolation() throws Exception {
assertThat(pickers.hasNext()).isFalse();
}

@Test(expected = IllegalArgumentException.class)
@Test
public void readyPicker_emptyList() {
// ready picker list must be non-empty
new ReadyPicker(Collections.<Subchannel>emptyList(), 0);
try {
new ReadyPicker(Collections.<Subchannel>emptyList(), 0);
fail();
} catch (IllegalArgumentException expected) {
}
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion rls/src/main/java/io/grpc/rls/RlsProtoConverters.java
Expand Up @@ -122,7 +122,7 @@ protected RouteLookupConfig doForward(Map<String, ?> json) {
String lookupService = JsonUtil.getString(json, "lookupService");
checkArgument(!Strings.isNullOrEmpty(lookupService), "lookupService must not be empty");
try {
new URI(lookupService);
URI unused = new URI(lookupService);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(
"The lookupService field is not valid URI: " + lookupService, e);
Expand Down

0 comments on commit 31ce764

Please sign in to comment.