Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcd20f57

Browse files
committed
refactor unit tests (exception handling)
1 parent54407f0 commitcd20f57

File tree

4 files changed

+80
-66
lines changed

4 files changed

+80
-66
lines changed

‎scribejava-apis/src/test/java/com/github/scribejava/apis/facebook/FacebookAccessTokenJsonExtractorTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
importjava.io.IOException;
55
importjava.util.Collections;
66
importstaticorg.junit.Assert.assertEquals;
7-
importstaticorg.junit.Assert.fail;
7+
importstaticorg.junit.Assert.assertThrows;
88
importorg.junit.Test;
9+
importorg.junit.function.ThrowingRunnable;
910

1011
publicclassFacebookAccessTokenJsonExtractorTest {
1112

@@ -19,9 +20,15 @@ public void shouldThrowExceptionIfResponseIsError() throws IOException {
1920
+"\"code\":100,"
2021
+"\"fbtrace_id\":\"DtxvtGRaxbB\"}}";
2122
try (Responseresponse =error(body)) {
22-
extractor.extract(response);
23-
fail();
24-
}catch (FacebookAccessTokenErrorResponsefateR) {
23+
24+
finalFacebookAccessTokenErrorResponsefateR =assertThrows(FacebookAccessTokenErrorResponse.class,
25+
newThrowingRunnable() {
26+
@Override
27+
publicvoidrun()throwsThrowable {
28+
extractor.extract(response);
29+
}
30+
});
31+
2532
assertEquals("This authorization code has been used.",fateR.getMessage());
2633
assertEquals("OAuthException",fateR.getType());
2734
assertEquals(100,fateR.getCodeInt());

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
importjava.util.Collections;
1111

1212
importstaticorg.junit.Assert.assertEquals;
13-
importstaticorg.junit.Assert.fail;
13+
importstaticorg.junit.Assert.assertThrows;
14+
importorg.junit.function.ThrowingRunnable;
1415

1516
publicclassOAuth2AccessTokenJsonExtractorTest {
1617

@@ -84,9 +85,13 @@ public void shouldThrowExceptionIfResponseIsError() throws IOException {
8485
+"\"error\":\"invalid_grant\""
8586
+"}";
8687
try (Responseresponse =error(responseBody)) {
87-
extractor.extract(response);
88-
fail();
89-
}catch (OAuth2AccessTokenErrorResponseoaer) {
88+
finalOAuth2AccessTokenErrorResponseoaer =assertThrows(OAuth2AccessTokenErrorResponse.class,
89+
newThrowingRunnable() {
90+
@Override
91+
publicvoidrun()throwsThrowable {
92+
extractor.extract(response);
93+
}
94+
});
9095
assertEquals(OAuth2Error.INVALID_GRANT,oaer.getError());
9196
assertEquals("unknown, invalid, or expired refresh token",oaer.getErrorDescription());
9297
}

‎scribejava-httpclient-apache/src/test/java/com/github/scribejava/httpclient/apache/OAuthAsyncCompletionHandlerTest.java

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
importstaticorg.junit.Assert.assertNotNull;
55
importstaticorg.junit.Assert.assertNull;
66
importstaticorg.junit.Assert.assertTrue;
7-
importstaticorg.junit.Assert.fail;
87

98
importjava.io.ByteArrayInputStream;
109
importjava.io.IOException;
@@ -23,6 +22,8 @@
2322
importcom.github.scribejava.core.model.OAuthAsyncRequestCallback;
2423
importcom.github.scribejava.core.model.OAuthRequest;
2524
importcom.github.scribejava.core.model.Response;
25+
importstaticorg.junit.Assert.assertThrows;
26+
importorg.junit.function.ThrowingRunnable;
2627

2728
publicclassOAuthAsyncCompletionHandlerTest {
2829

@@ -80,7 +81,7 @@ public void shouldReleaseLatchOnSuccess() throws Exception {
8081
}
8182

8283
@Test
83-
publicvoidshouldReleaseLatchOnIOException()throwsException{
84+
publicvoidshouldReleaseLatchOnIOException() {
8485
handler =newOAuthAsyncCompletionHandler<>(callback,EXCEPTION_RESPONSE_CONVERTER);
8586
finalHttpResponseresponse
8687
=newBasicHttpResponse(newBasicStatusLine(newProtocolVersion("4",1,1),200,"ok"));
@@ -92,16 +93,16 @@ public void shouldReleaseLatchOnIOException() throws Exception {
9293
assertNotNull(callback.getThrowable());
9394
assertTrue(callback.getThrowable()instanceofIOException);
9495
// verify latch is released
95-
try {
96-
handler.getResult();
97-
fail();
98-
}catch (ExecutionExceptionexpected) {
99-
// expected
100-
}
96+
assertThrows(ExecutionException.class,newThrowingRunnable() {
97+
@Override
98+
publicvoidrun()throwsThrowable {
99+
handler.getResult();
100+
}
101+
});
101102
}
102103

103104
@Test
104-
publicvoidshouldReportOAuthException()throwsException{
105+
publicvoidshouldReportOAuthException() {
105106
handler =newOAuthAsyncCompletionHandler<>(callback,OAUTH_EXCEPTION_RESPONSE_CONVERTER);
106107
finalHttpResponseresponse
107108
=newBasicHttpResponse(newBasicStatusLine(newProtocolVersion("4",1,1),200,"ok"));
@@ -113,16 +114,16 @@ public void shouldReportOAuthException() throws Exception {
113114
assertNotNull(callback.getThrowable());
114115
assertTrue(callback.getThrowable()instanceofOAuthException);
115116
// verify latch is released
116-
try {
117-
handler.getResult();
118-
fail();
119-
}catch (ExecutionExceptionexpected) {
120-
// expected
121-
}
117+
assertThrows(ExecutionException.class,newThrowingRunnable() {
118+
@Override
119+
publicvoidrun()throwsThrowable {
120+
handler.getResult();
121+
}
122+
});
122123
}
123124

124125
@Test
125-
publicvoidshouldReleaseLatchOnCancel()throwsException{
126+
publicvoidshouldReleaseLatchOnCancel() {
126127
handler =newOAuthAsyncCompletionHandler<>(callback,ALL_GOOD_RESPONSE_CONVERTER);
127128
finalHttpResponseresponse
128129
=newBasicHttpResponse(newBasicStatusLine(newProtocolVersion("4",1,1),200,"ok"));
@@ -134,16 +135,16 @@ public void shouldReleaseLatchOnCancel() throws Exception {
134135
assertNotNull(callback.getThrowable());
135136
assertTrue(callback.getThrowable()instanceofCancellationException);
136137
// verify latch is released
137-
try {
138-
handler.getResult();
139-
fail();
140-
}catch (ExecutionExceptionexpected) {
141-
// expected
142-
}
138+
assertThrows(ExecutionException.class,newThrowingRunnable() {
139+
@Override
140+
publicvoidrun()throwsThrowable {
141+
handler.getResult();
142+
}
143+
});
143144
}
144145

145146
@Test
146-
publicvoidshouldReleaseLatchOnFailure()throwsException{
147+
publicvoidshouldReleaseLatchOnFailure() {
147148
handler =newOAuthAsyncCompletionHandler<>(callback,ALL_GOOD_RESPONSE_CONVERTER);
148149
finalHttpResponseresponse
149150
=newBasicHttpResponse(newBasicStatusLine(newProtocolVersion("4",1,1),200,"ok"));
@@ -155,12 +156,12 @@ public void shouldReleaseLatchOnFailure() throws Exception {
155156
assertNotNull(callback.getThrowable());
156157
assertTrue(callback.getThrowable()instanceofRuntimeException);
157158
// verify latch is released
158-
try {
159-
handler.getResult();
160-
fail();
161-
}catch (ExecutionExceptionexpected) {
162-
// expected
163-
}
159+
assertThrows(ExecutionException.class,newThrowingRunnable() {
160+
@Override
161+
publicvoidrun()throwsThrowable {
162+
handler.getResult();
163+
}
164+
});
164165
}
165166

166167
privatestaticclassAllGoodResponseConverterimplementsOAuthRequest.ResponseConverter<String> {

‎scribejava-httpclient-okhttp/src/test/java/com/github/scribejava/httpclient/okhttp/OAuthAsyncCompletionHandlerTest.java

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
importstaticorg.junit.Assert.assertNotNull;
55
importstaticorg.junit.Assert.assertNull;
66
importstaticorg.junit.Assert.assertTrue;
7-
importstaticorg.junit.Assert.fail;
7+
importstaticorg.junit.Assert.assertThrows;
88

99
importjava.io.IOException;
1010
importjava.util.concurrent.ExecutionException;
@@ -22,6 +22,7 @@
2222
importokhttp3.Protocol;
2323
importokhttp3.Request;
2424
importokhttp3.ResponseBody;
25+
importorg.junit.function.ThrowingRunnable;
2526

2627
publicclassOAuthAsyncCompletionHandlerTest {
2728

@@ -88,7 +89,7 @@ public void shouldReleaseLatchOnSuccess() throws Exception {
8889
}
8990

9091
@Test
91-
publicvoidshouldReleaseLatchOnIOException()throwsException{
92+
publicvoidshouldReleaseLatchOnIOException() {
9293
handler =newOAuthAsyncCompletionHandler<>(callback,EXCEPTION_RESPONSE_CONVERTER,future);
9394
call.enqueue(handler);
9495

@@ -105,16 +106,16 @@ public void shouldReleaseLatchOnIOException() throws Exception {
105106
assertNotNull(callback.getThrowable());
106107
assertTrue(callback.getThrowable()instanceofIOException);
107108
// verify latch is released
108-
try {
109-
future.get();
110-
fail();
111-
}catch (ExecutionExceptionexpected) {
112-
// expected
113-
}
109+
assertThrows(ExecutionException.class,newThrowingRunnable() {
110+
@Override
111+
publicvoidrun()throwsThrowable {
112+
future.get();
113+
}
114+
});
114115
}
115116

116117
@Test
117-
publicvoidshouldReportOAuthException()throwsException{
118+
publicvoidshouldReportOAuthException() {
118119
handler =newOAuthAsyncCompletionHandler<>(callback,OAUTH_EXCEPTION_RESPONSE_CONVERTER,future);
119120
call.enqueue(handler);
120121

@@ -131,16 +132,16 @@ public void shouldReportOAuthException() throws Exception {
131132
assertNotNull(callback.getThrowable());
132133
assertTrue(callback.getThrowable()instanceofOAuthException);
133134
// verify latch is released
134-
try {
135-
future.get();
136-
fail();
137-
}catch (ExecutionExceptionexpected) {
138-
// expected
139-
}
135+
assertThrows(ExecutionException.class,newThrowingRunnable() {
136+
@Override
137+
publicvoidrun()throwsThrowable {
138+
future.get();
139+
}
140+
});
140141
}
141142

142143
@Test
143-
publicvoidshouldReleaseLatchOnCancel()throwsException{
144+
publicvoidshouldReleaseLatchOnCancel() {
144145
handler =newOAuthAsyncCompletionHandler<>(callback,ALL_GOOD_RESPONSE_CONVERTER,future);
145146
call.enqueue(handler);
146147

@@ -149,16 +150,16 @@ public void shouldReleaseLatchOnCancel() throws Exception {
149150
assertNotNull(callback.getThrowable());
150151
assertTrue(callback.getThrowable()instanceofIOException);
151152
// verify latch is released
152-
try {
153-
future.get();
154-
fail();
155-
}catch (ExecutionExceptionexpected) {
156-
// expected
157-
}
153+
assertThrows(ExecutionException.class,newThrowingRunnable() {
154+
@Override
155+
publicvoidrun()throwsThrowable {
156+
future.get();
157+
}
158+
});
158159
}
159160

160161
@Test
161-
publicvoidshouldReleaseLatchOnFailure()throwsException{
162+
publicvoidshouldReleaseLatchOnFailure() {
162163
handler =newOAuthAsyncCompletionHandler<>(callback,ALL_GOOD_RESPONSE_CONVERTER,future);
163164
call.enqueue(handler);
164165

@@ -167,12 +168,12 @@ public void shouldReleaseLatchOnFailure() throws Exception {
167168
assertNotNull(callback.getThrowable());
168169
assertTrue(callback.getThrowable()instanceofIOException);
169170
// verify latch is released
170-
try {
171-
future.get();
172-
fail();
173-
}catch (ExecutionExceptionexpected) {
174-
// expected
175-
}
171+
assertThrows(ExecutionException.class,newThrowingRunnable() {
172+
@Override
173+
publicvoidrun()throwsThrowable {
174+
future.get();
175+
}
176+
});
176177
}
177178

178179
privatestaticclassAllGoodResponseConverterimplementsOAuthRequest.ResponseConverter<String> {

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp