Skip to content

Commit 570f8bb

Browse files
Verify exception messages in tests (#566)
Improve test coverage and make refactorings more safe. Signed-off-by: Stefan Birkner <[email protected]>
1 parent f48acbb commit 570f8bb

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

simpleclient/src/test/java/io/prometheus/client/CounterTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
package io.prometheus.client;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.rules.ExpectedException.none;
45

56
import java.util.ArrayList;
67
import java.util.List;
8+
9+
import org.junit.Rule;
710
import org.junit.Test;
811
import org.junit.Before;
12+
import org.junit.rules.ExpectedException;
913

1014

1115
public class CounterTest {
1216
CollectorRegistry registry;
1317
Counter noLabels, labels;
1418

19+
@Rule
20+
public final ExpectedException thrown = none();
21+
1522
@Before
1623
public void setUp() {
1724
registry = new CollectorRegistry();
@@ -39,8 +46,10 @@ public void testIncrement() {
3946
assertEquals(8.0, noLabels.get(), .001);
4047
}
4148

42-
@Test(expected=IllegalArgumentException.class)
49+
@Test
4350
public void testNegativeIncrementFails() {
51+
thrown.expect(IllegalArgumentException.class);
52+
thrown.expectMessage("Amount to increment must be non-negative.");
4453
noLabels.inc(-1);
4554
}
4655

simpleclient/src/test/java/io/prometheus/client/HistogramTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertEquals;
55
import static org.junit.Assert.assertTrue;
6+
import static org.junit.rules.ExpectedException.none;
67

78
import java.lang.reflect.Method;
89
import java.lang.reflect.Modifier;
@@ -11,14 +12,20 @@
1112
import java.util.concurrent.Callable;
1213
import org.junit.After;
1314
import org.junit.Before;
15+
import org.junit.Rule;
1416
import org.junit.Test;
17+
import org.junit.rules.ExpectedException;
1518

1619

1720
public class HistogramTest {
1821

1922
CollectorRegistry registry;
2023
Histogram noLabels, labels;
2124

25+
26+
@Rule
27+
public final ExpectedException thrown = none();
28+
2229
@Before
2330
public void setUp() {
2431
registry = new CollectorRegistry();
@@ -166,8 +173,10 @@ public void testLabels() {
166173
assertEquals(3.0, getLabelsSum("b").doubleValue(), .001);
167174
}
168175

169-
@Test(expected=IllegalStateException.class)
176+
@Test
170177
public void testLeLabelThrows() {
178+
thrown.expect(IllegalStateException.class);
179+
thrown.expectMessage("Histogram cannot have a label named 'le'.");
171180
Histogram.build().name("labels").help("help").labelNames("le").create();
172181
}
173182

simpleclient/src/test/java/io/prometheus/client/SimpleCollectorTest.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNull;
55
import static org.junit.Assert.assertNotNull;
6+
import static org.junit.rules.ExpectedException.none;
67

8+
import org.junit.Rule;
79
import org.junit.Test;
810
import org.junit.Before;
11+
import org.junit.rules.ExpectedException;
912

1013

1114
public class SimpleCollectorTest {
@@ -14,6 +17,9 @@ public class SimpleCollectorTest {
1417
Gauge metric;
1518
Gauge noLabels;
1619

20+
@Rule
21+
public final ExpectedException thrown = none();
22+
1723
@Before
1824
public void setUp() {
1925
registry = new CollectorRegistry();
@@ -29,18 +35,24 @@ private Double getValueNoLabels() {
2935
return registry.getSampleValue("nolabels");
3036
}
3137

32-
@Test(expected=IllegalArgumentException.class)
38+
@Test
3339
public void testTooFewLabelsThrows() {
40+
thrown.expect(IllegalArgumentException.class);
41+
thrown.expectMessage("Incorrect number of labels.");
3442
metric.labels();
3543
}
3644

37-
@Test(expected=IllegalArgumentException.class)
45+
@Test
3846
public void testNullLabelThrows() {
47+
thrown.expect(IllegalArgumentException.class);
48+
thrown.expectMessage("Label cannot be null.");
3949
metric.labels(new String[]{null});
4050
}
4151

42-
@Test(expected=IllegalArgumentException.class)
52+
@Test
4353
public void testTooManyLabelsThrows() {
54+
thrown.expect(IllegalArgumentException.class);
55+
thrown.expectMessage("Incorrect number of labels.");
4456
metric.labels("a", "b");
4557
}
4658

@@ -97,28 +109,39 @@ public void testNameIsConcatenated() {
97109
assertEquals("a_b_c", Gauge.build().name("c").subsystem("b").namespace("a").help("h").create().fullname);
98110
}
99111

100-
@Test(expected=IllegalStateException.class)
112+
@Test
101113
public void testNameIsRequired() {
114+
thrown.expect(IllegalStateException.class);
115+
thrown.expectMessage("Name hasn't been set.");
102116
Gauge.build().help("h").create();
103117
}
104118

105-
@Test(expected=IllegalStateException.class)
119+
@Test
106120
public void testHelpIsRequired() {
121+
thrown.expect(IllegalStateException.class);
122+
thrown.expectMessage("Help hasn't been set.");
107123
Gauge.build().name("c").create();
108124
}
109125

110-
@Test(expected=IllegalArgumentException.class)
126+
@Test
111127
public void testInvalidNameThrows() {
128+
thrown.expect(IllegalArgumentException.class);
129+
thrown.expectMessage("Invalid metric name: c'a");
112130
Gauge.build().name("c'a").create();
113131
}
114132

115-
@Test(expected=IllegalArgumentException.class)
133+
@Test
116134
public void testInvalidLabelNameThrows() {
135+
thrown.expect(IllegalArgumentException.class);
136+
thrown.expectMessage("Invalid metric label name: c:d");
117137
Gauge.build().name("a").labelNames("c:d").help("h").create();
118138
}
119139

120-
@Test(expected=IllegalArgumentException.class)
140+
@Test
121141
public void testReservedLabelNameThrows() {
142+
thrown.expect(IllegalArgumentException.class);
143+
thrown.expectMessage(
144+
"Invalid metric label name, reserved for internal use: __name__");
122145
Gauge.build().name("a").labelNames("__name__").help("h").create();
123146
}
124147

simpleclient_pushgateway/src/test/java/io/prometheus/client/exporter/PushGatewayTest.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.prometheus.client.exporter;
22

33

4+
import static org.junit.rules.ExpectedException.none;
45
import static org.mockserver.model.HttpRequest.request;
56
import static org.mockserver.model.HttpResponse.response;
67

@@ -13,11 +14,16 @@
1314
import org.junit.Before;
1415
import org.junit.Rule;
1516
import org.junit.Test;
17+
import org.junit.rules.ExpectedException;
1618
import org.mockserver.junit.MockServerRule;
1719
import org.mockserver.client.server.MockServerClient;
1820

1921
public class PushGatewayTest {
2022

23+
24+
@Rule
25+
public final ExpectedException thrown = none();
26+
2127
@Rule
2228
public MockServerRule mockServerRule = new MockServerRule(this);
2329
private MockServerClient mockServerClient;
@@ -70,13 +76,18 @@ public void testPush200Response() throws IOException {
7076
pg.push(registry, "j");
7177
}
7278

73-
@Test(expected=IOException.class)
79+
@Test
7480
public void testNon202ResponseThrows() throws IOException {
7581
mockServerClient.when(
7682
request()
7783
.withMethod("PUT")
7884
.withPath("/metrics/job/j")
7985
).respond(response().withStatusCode(500));
86+
thrown.expect(IOException.class);
87+
thrown.expectMessage(
88+
"Response code from http://localhost:"
89+
+ mockServerRule.getHttpPort()
90+
+ "/metrics/job/j was 500");
8091
pg.push(registry, "j");
8192
}
8293

@@ -223,13 +234,18 @@ public void testOldPushWithInstance() throws IOException {
223234
pg.push(registry, "j", "i");
224235
}
225236

226-
@Test(expected=IOException.class)
237+
@Test
227238
public void testOldNon202ResponseThrows() throws IOException {
228239
mockServerClient.when(
229240
request()
230241
.withMethod("PUT")
231242
.withPath("/metrics/job/j/instance/i")
232243
).respond(response().withStatusCode(500));
244+
thrown.expect(IOException.class);
245+
thrown.expectMessage(
246+
"Response code from http://localhost:"
247+
+ mockServerRule.getHttpPort()
248+
+ "/metrics/job/j/instance/i was 500");
233249
pg.push(registry,"j", "i");
234250
}
235251

0 commit comments

Comments
 (0)