Use backported JUnit exceptThrows and assertThrows in metrics library

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=176584707
This commit is contained in:
jianglai 2017-11-21 17:26:07 -08:00
parent 6eb0d8689d
commit a92cdbe8c3
17 changed files with 325 additions and 226 deletions

View file

@ -15,22 +15,18 @@
package google.registry.monitoring.metrics;
import static com.google.common.truth.Truth.assertThat;
import static google.registry.monitoring.metrics.JUnitBackports.expectThrows;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.joda.time.Instant;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/** Unit tests for {@link Counter}. */
@RunWith(JUnit4.class)
public class CounterTest {
@Rule public final ExpectedException thrown = ExpectedException.none();
@Test
public void testGetCardinality_reflectsCurrentCardinality() {
Counter counter =
@ -59,11 +55,12 @@ public class CounterTest {
ImmutableSet.of(
LabelDescriptor.create("label1", "bar"), LabelDescriptor.create("label2", "bar")));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage(
"The count of labelValues must be equal to the underlying Metric's count of labels.");
counter.increment("blah");
IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> counter.increment("blah"));
assertThat(thrown)
.hasMessageThat()
.contains(
"The count of labelValues must be equal to the underlying Metric's count of labels.");
}
@Test
@ -122,9 +119,9 @@ public class CounterTest {
"vdn",
ImmutableSet.of(LabelDescriptor.create("label1", "bar")));
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("The offset provided must be non-negative");
counter.incrementBy(-1L, "foo");
IllegalArgumentException thrown =
expectThrows(IllegalArgumentException.class, () -> counter.incrementBy(-1L, "foo"));
assertThat(thrown).hasMessageThat().contains("The offset provided must be non-negative");
}
@Test