Skip to content

Commit fe3e14a

Browse files
committed
switch to assertThrows from @test(expected = ...) in unit tests
1 parent 5a0df76 commit fe3e14a

File tree

11 files changed

+188
-64
lines changed

11 files changed

+188
-64
lines changed

scribejava-core/src/test/java/com/github/scribejava/core/extractors/BaseStringExtractorTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.github.scribejava.core.exceptions.OAuthParametersMissingException;
88
import com.github.scribejava.core.model.OAuthRequest;
99
import com.github.scribejava.core.model.Verb;
10+
import static org.junit.Assert.assertThrows;
11+
import org.junit.function.ThrowingRunnable;
1012

1113
public class BaseStringExtractorTest {
1214

@@ -83,15 +85,23 @@ public void shouldExcludePort443v2() {
8385
assertEquals(expected, baseString);
8486
}
8587

86-
@Test(expected = IllegalArgumentException.class)
8788
public void shouldThrowExceptionIfRquestIsNull() {
88-
extractor.extract(null);
89+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
90+
@Override
91+
public void run() throws Throwable {
92+
extractor.extract(null);
93+
}
94+
});
8995
}
9096

91-
@Test(expected = OAuthParametersMissingException.class)
9297
public void shouldThrowExceptionIfRquestHasNoOAuthParameters() {
9398
final OAuthRequest request = new OAuthRequest(Verb.GET, "http://example.com");
94-
extractor.extract(request);
99+
assertThrows(OAuthParametersMissingException.class, new ThrowingRunnable() {
100+
@Override
101+
public void run() throws Throwable {
102+
extractor.extract(request);
103+
}
104+
});
95105
}
96106

97107
@Test

scribejava-core/src/test/java/com/github/scribejava/core/extractors/HeaderExtractorTest.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertTrue;
5+
import static org.junit.Assert.assertThrows;
56
import org.junit.Before;
67
import org.junit.Test;
78
import com.github.scribejava.core.exceptions.OAuthParametersMissingException;
89
import com.github.scribejava.core.model.OAuthRequest;
910
import com.github.scribejava.core.model.Verb;
1011
import com.github.scribejava.core.ObjectMother;
12+
import org.junit.function.ThrowingRunnable;
1113

1214
public class HeaderExtractorTest {
1315

@@ -36,21 +38,29 @@ public void shouldExtractStandardHeader() {
3638
assertTrue(header.contains(timestamp));
3739
// Assert that header only contains the checked elements above and nothing else
3840
assertEquals(", , , ",
39-
header.replaceFirst(oauth, "")
40-
.replaceFirst(callback, "")
41-
.replaceFirst(signature, "")
42-
.replaceFirst(key, "")
43-
.replaceFirst(timestamp, ""));
41+
header.replaceFirst(oauth, "")
42+
.replaceFirst(callback, "")
43+
.replaceFirst(signature, "")
44+
.replaceFirst(key, "")
45+
.replaceFirst(timestamp, ""));
4446
}
4547

46-
@Test(expected = IllegalArgumentException.class)
4748
public void shouldExceptionIfRequestIsNull() {
48-
extractor.extract(null);
49+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
50+
@Override
51+
public void run() throws Throwable {
52+
extractor.extract(null);
53+
}
54+
});
4955
}
5056

51-
@Test(expected = OAuthParametersMissingException.class)
5257
public void shouldExceptionIfRequestHasNoOAuthParams() {
5358
final OAuthRequest emptyRequest = new OAuthRequest(Verb.GET, "http://example.com");
54-
extractor.extract(emptyRequest);
59+
assertThrows(OAuthParametersMissingException.class, new ThrowingRunnable() {
60+
@Override
61+
public void run() throws Throwable {
62+
extractor.extract(emptyRequest);
63+
}
64+
});
5565
}
5666
}

scribejava-core/src/test/java/com/github/scribejava/core/extractors/OAuth1AccessTokenExtractorTest.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.Collections;
1111

1212
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertThrows;
14+
import org.junit.function.ThrowingRunnable;
1315

1416
public class OAuth1AccessTokenExtractorTest {
1517

@@ -65,34 +67,50 @@ public void shouldExtractTokenWithEmptySecret() throws IOException {
6567
assertEquals("", extracted.getTokenSecret());
6668
}
6769

68-
@Test(expected = OAuthException.class)
6970
public void shouldThrowExceptionIfTokenIsAbsent() throws IOException {
7071
final String responseBody = "oauth_secret=hh5s93j4hdidpola&callback_confirmed=true";
7172
try (Response response = ok(responseBody)) {
72-
extractor.extract(response);
73+
assertThrows(OAuthException.class, new ThrowingRunnable() {
74+
@Override
75+
public void run() throws Throwable {
76+
extractor.extract(response);
77+
}
78+
});
7379
}
7480
}
7581

76-
@Test(expected = OAuthException.class)
7782
public void shouldThrowExceptionIfSecretIsAbsent() throws IOException {
7883
final String responseBody = "oauth_token=hh5s93j4hdidpola&callback_confirmed=true";
7984
try (Response response = ok(responseBody)) {
80-
extractor.extract(response);
85+
assertThrows(OAuthException.class, new ThrowingRunnable() {
86+
@Override
87+
public void run() throws Throwable {
88+
extractor.extract(response);
89+
}
90+
});
8191
}
8292
}
8393

84-
@Test(expected = IllegalArgumentException.class)
8594
public void shouldThrowExceptionIfResponseIsNull() throws IOException {
8695
try (Response response = ok(null)) {
87-
extractor.extract(response);
96+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
97+
@Override
98+
public void run() throws Throwable {
99+
extractor.extract(response);
100+
}
101+
});
88102
}
89103
}
90104

91-
@Test(expected = IllegalArgumentException.class)
92105
public void shouldThrowExceptionIfResponseIsEmptyString() throws IOException {
93106
final String responseBody = "";
94107
try (Response response = ok(responseBody)) {
95-
extractor.extract(response);
108+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
109+
@Override
110+
public void run() throws Throwable {
111+
extractor.extract(response);
112+
}
113+
});
96114
}
97115
}
98116

scribejava-core/src/test/java/com/github/scribejava/core/extractors/OAuth2AccessTokenExtractorTest.java

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.Collections;
1111

1212
import static org.junit.Assert.assertEquals;
13+
import static org.junit.Assert.assertThrows;
14+
import org.junit.function.ThrowingRunnable;
1315

1416
public class OAuth2AccessTokenExtractorTest {
1517

@@ -70,34 +72,50 @@ public void shouldExtractTokenFromResponseWithManyParameters() throws IOExceptio
7072
assertEquals("foo1234", extracted.getAccessToken());
7173
}
7274

73-
@Test(expected = OAuthException.class)
7475
public void shouldThrowExceptionIfErrorResponse() throws IOException {
7576
final String responseBody = "";
7677
try (Response response = error(responseBody)) {
77-
extractor.extract(response);
78+
assertThrows(OAuthException.class, new ThrowingRunnable() {
79+
@Override
80+
public void run() throws Throwable {
81+
extractor.extract(response);
82+
}
83+
});
7884
}
7985
}
8086

81-
@Test(expected = OAuthException.class)
8287
public void shouldThrowExceptionIfTokenIsAbsent() throws IOException {
8388
final String responseBody = "&expires=5108";
8489
try (Response response = ok(responseBody)) {
85-
extractor.extract(response);
90+
assertThrows(OAuthException.class, new ThrowingRunnable() {
91+
@Override
92+
public void run() throws Throwable {
93+
extractor.extract(response);
94+
}
95+
});
8696
}
8797
}
8898

89-
@Test(expected = IllegalArgumentException.class)
9099
public void shouldThrowExceptionIfResponseIsNull() throws IOException {
91100
try (Response response = ok(null)) {
92-
extractor.extract(response);
101+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
102+
@Override
103+
public void run() throws Throwable {
104+
extractor.extract(response);
105+
}
106+
});
93107
}
94108
}
95109

96-
@Test(expected = IllegalArgumentException.class)
97110
public void shouldThrowExceptionIfResponseIsEmptyString() throws IOException {
98111
final String responseBody = "";
99112
try (Response response = ok(responseBody)) {
100-
extractor.extract(response);
113+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
114+
@Override
115+
public void run() throws Throwable {
116+
extractor.extract(response);
117+
}
118+
});
101119
}
102120
}
103121

scribejava-core/src/test/java/com/github/scribejava/core/extractors/OAuth2AccessTokenJsonExtractorTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,26 @@ public void shouldParseScopeFromResponse() throws IOException {
6363
assertEquals("refresh_token1", token3.getRefreshToken());
6464
}
6565

66-
@Test(expected = IllegalArgumentException.class)
6766
public void shouldThrowExceptionIfForNullParameters() throws IOException {
6867
try (Response response = ok(null)) {
69-
extractor.extract(response);
68+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
69+
@Override
70+
public void run() throws Throwable {
71+
extractor.extract(response);
72+
}
73+
});
7074
}
7175
}
7276

73-
@Test(expected = IllegalArgumentException.class)
7477
public void shouldThrowExceptionIfForEmptyStrings() throws IOException {
7578
final String responseBody = "";
7679
try (Response response = ok(responseBody)) {
77-
extractor.extract(response);
80+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
81+
@Override
82+
public void run() throws Throwable {
83+
extractor.extract(response);
84+
}
85+
});
7886
}
7987
}
8088

scribejava-core/src/test/java/com/github/scribejava/core/model/OAuthRequestTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.github.scribejava.core.model;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertThrows;
45
import static org.junit.Assert.assertTrue;
56
import org.junit.Before;
67
import org.junit.Test;
8+
import org.junit.function.ThrowingRunnable;
79

810
public class OAuthRequestTest {
911

@@ -25,9 +27,13 @@ public void shouldAddOAuthParamters() {
2527
assertEquals(5, request.getOauthParameters().size());
2628
}
2729

28-
@Test(expected = IllegalArgumentException.class)
2930
public void shouldThrowExceptionIfParameterIsNotOAuth() {
30-
request.addOAuthParameter("otherParam", "value");
31+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
32+
@Override
33+
public void run() throws Throwable {
34+
request.addOAuthParameter("otherParam", "value");
35+
}
36+
});
3137
}
3238

3339
@Test

scribejava-core/src/test/java/com/github/scribejava/core/model/ParameterListTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.junit.Test;
66

77
import static org.junit.Assert.assertNotSame;
8+
import static org.junit.Assert.assertThrows;
9+
import org.junit.function.ThrowingRunnable;
810

911
public class ParameterListTest {
1012

@@ -15,9 +17,13 @@ public void setUp() {
1517
this.params = new ParameterList();
1618
}
1719

18-
@Test(expected = IllegalArgumentException.class)
1920
public void shouldThrowExceptionWhenAppendingNullMapToQuerystring() {
20-
params.appendTo(null);
21+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
22+
@Override
23+
public void run() throws Throwable {
24+
params.appendTo(null);
25+
}
26+
});
2127
}
2228

2329
@Test

scribejava-core/src/test/java/com/github/scribejava/core/services/HMACSha1SignatureServiceTest.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.junit.Before;
55
import org.junit.Test;
66
import com.github.scribejava.core.exceptions.OAuthException;
7+
import static org.junit.Assert.assertThrows;
8+
import org.junit.function.ThrowingRunnable;
79

810
public class HMACSha1SignatureServiceTest {
911

@@ -29,19 +31,31 @@ public void shouldReturnSignature() {
2931
assertEquals(signature, service.getSignature(baseString, apiSecret, tokenSecret));
3032
}
3133

32-
@Test(expected = OAuthException.class)
3334
public void shouldThrowExceptionIfBaseStringIsNull() {
34-
service.getSignature(null, "apiSecret", "tokenSecret");
35+
assertThrows(OAuthException.class, new ThrowingRunnable() {
36+
@Override
37+
public void run() throws Throwable {
38+
service.getSignature(null, "apiSecret", "tokenSecret");
39+
}
40+
});
3541
}
3642

37-
@Test(expected = OAuthException.class)
3843
public void shouldThrowExceptionIfBaseStringIsEmpty() {
39-
service.getSignature(" ", "apiSecret", "tokenSecret");
44+
assertThrows(OAuthException.class, new ThrowingRunnable() {
45+
@Override
46+
public void run() throws Throwable {
47+
service.getSignature(" ", "apiSecret", "tokenSecret");
48+
}
49+
});
4050
}
4151

42-
@Test(expected = OAuthException.class)
4352
public void shouldThrowExceptionIfApiSecretIsNull() {
44-
service.getSignature("base string", null, "tokenSecret");
53+
assertThrows(OAuthException.class, new ThrowingRunnable() {
54+
@Override
55+
public void run() throws Throwable {
56+
service.getSignature("base string", null, "tokenSecret");
57+
}
58+
});
4559
}
4660

4761
public void shouldNotThrowExceptionIfApiSecretIsEmpty() {

scribejava-core/src/test/java/com/github/scribejava/core/utils/OAuthEncoderTest.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.github.scribejava.core.utils;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertThrows;
45
import org.junit.Test;
6+
import org.junit.function.ThrowingRunnable;
57

68
public class OAuthEncoderTest {
79

@@ -34,14 +36,22 @@ public void shouldNotPercentEncodeReservedCharacters() {
3436
assertEquals(encoded, OAuthEncoder.encode(plain));
3537
}
3638

37-
@Test(expected = IllegalArgumentException.class)
3839
public void shouldThrowExceptionIfStringToEncodeIsNull() {
39-
OAuthEncoder.encode(null);
40+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
41+
@Override
42+
public void run() throws Throwable {
43+
OAuthEncoder.encode(null);
44+
}
45+
});
4046
}
4147

42-
@Test(expected = IllegalArgumentException.class)
4348
public void shouldThrowExceptionIfStringToDecodeIsNull() {
44-
OAuthEncoder.decode(null);
49+
assertThrows(IllegalArgumentException.class, new ThrowingRunnable() {
50+
@Override
51+
public void run() throws Throwable {
52+
OAuthEncoder.decode(null);
53+
}
54+
});
4555
}
4656

4757
@Test

0 commit comments

Comments
 (0)