Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd20f57

Browse files
committed
refactor unit tests (exception handling)
1 parent 54407f0 commit cd20f57

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
import java.io.IOException;
55
import java.util.Collections;
66
import static org.junit.Assert.assertEquals;
7-
import static org.junit.Assert.fail;
7+
import static org.junit.Assert.assertThrows;
88
import org.junit.Test;
9+
import org.junit.function.ThrowingRunnable;
910

1011
public class FacebookAccessTokenJsonExtractorTest {
1112

@@ -19,9 +20,15 @@ public void shouldThrowExceptionIfResponseIsError() throws IOException {
1920
+ "\"code\":100,"
2021
+ "\"fbtrace_id\":\"DtxvtGRaxbB\"}}";
2122
try (Response response = error(body)) {
22-
extractor.extract(response);
23-
fail();
24-
} catch (FacebookAccessTokenErrorResponse fateR) {
23+
24+
final FacebookAccessTokenErrorResponse fateR = assertThrows(FacebookAccessTokenErrorResponse.class,
25+
new ThrowingRunnable() {
26+
@Override
27+
public void run() throws Throwable {
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
import java.util.Collections;
1111

1212
import static org.junit.Assert.assertEquals;
13-
import static org.junit.Assert.fail;
13+
import static org.junit.Assert.assertThrows;
14+
import org.junit.function.ThrowingRunnable;
1415

1516
public class OAuth2AccessTokenJsonExtractorTest {
1617

@@ -84,9 +85,13 @@ public void shouldThrowExceptionIfResponseIsError() throws IOException {
8485
+ "\"error\":\"invalid_grant\""
8586
+ "}";
8687
try (Response response = error(responseBody)) {
87-
extractor.extract(response);
88-
fail();
89-
} catch (OAuth2AccessTokenErrorResponse oaer) {
88+
final OAuth2AccessTokenErrorResponse oaer = assertThrows(OAuth2AccessTokenErrorResponse.class,
89+
new ThrowingRunnable() {
90+
@Override
91+
public void run() throws Throwable {
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
import static org.junit.Assert.assertNotNull;
55
import static org.junit.Assert.assertNull;
66
import static org.junit.Assert.assertTrue;
7-
import static org.junit.Assert.fail;
87

98
import java.io.ByteArrayInputStream;
109
import java.io.IOException;
@@ -23,6 +22,8 @@
2322
import com.github.scribejava.core.model.OAuthAsyncRequestCallback;
2423
import com.github.scribejava.core.model.OAuthRequest;
2524
import com.github.scribejava.core.model.Response;
25+
import static org.junit.Assert.assertThrows;
26+
import org.junit.function.ThrowingRunnable;
2627

2728
public class OAuthAsyncCompletionHandlerTest {
2829

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

8283
@Test
83-
public void shouldReleaseLatchOnIOException() throws Exception {
84+
public void shouldReleaseLatchOnIOException() {
8485
handler = new OAuthAsyncCompletionHandler<>(callback, EXCEPTION_RESPONSE_CONVERTER);
8586
final HttpResponse response
8687
= new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
@@ -92,16 +93,16 @@ public void shouldReleaseLatchOnIOException() throws Exception {
9293
assertNotNull(callback.getThrowable());
9394
assertTrue(callback.getThrowable() instanceof IOException);
9495
// verify latch is released
95-
try {
96-
handler.getResult();
97-
fail();
98-
} catch (ExecutionException expected) {
99-
// expected
100-
}
96+
assertThrows(ExecutionException.class, new ThrowingRunnable() {
97+
@Override
98+
public void run() throws Throwable {
99+
handler.getResult();
100+
}
101+
});
101102
}
102103

103104
@Test
104-
public void shouldReportOAuthException() throws Exception {
105+
public void shouldReportOAuthException() {
105106
handler = new OAuthAsyncCompletionHandler<>(callback, OAUTH_EXCEPTION_RESPONSE_CONVERTER);
106107
final HttpResponse response
107108
= new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
@@ -113,16 +114,16 @@ public void shouldReportOAuthException() throws Exception {
113114
assertNotNull(callback.getThrowable());
114115
assertTrue(callback.getThrowable() instanceof OAuthException);
115116
// verify latch is released
116-
try {
117-
handler.getResult();
118-
fail();
119-
} catch (ExecutionException expected) {
120-
// expected
121-
}
117+
assertThrows(ExecutionException.class, new ThrowingRunnable() {
118+
@Override
119+
public void run() throws Throwable {
120+
handler.getResult();
121+
}
122+
});
122123
}
123124

124125
@Test
125-
public void shouldReleaseLatchOnCancel() throws Exception {
126+
public void shouldReleaseLatchOnCancel() {
126127
handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
127128
final HttpResponse response
128129
= new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
@@ -134,16 +135,16 @@ public void shouldReleaseLatchOnCancel() throws Exception {
134135
assertNotNull(callback.getThrowable());
135136
assertTrue(callback.getThrowable() instanceof CancellationException);
136137
// verify latch is released
137-
try {
138-
handler.getResult();
139-
fail();
140-
} catch (ExecutionException expected) {
141-
// expected
142-
}
138+
assertThrows(ExecutionException.class, new ThrowingRunnable() {
139+
@Override
140+
public void run() throws Throwable {
141+
handler.getResult();
142+
}
143+
});
143144
}
144145

145146
@Test
146-
public void shouldReleaseLatchOnFailure() throws Exception {
147+
public void shouldReleaseLatchOnFailure() {
147148
handler = new OAuthAsyncCompletionHandler<>(callback, ALL_GOOD_RESPONSE_CONVERTER);
148149
final HttpResponse response
149150
= new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("4", 1, 1), 200, "ok"));
@@ -155,12 +156,12 @@ public void shouldReleaseLatchOnFailure() throws Exception {
155156
assertNotNull(callback.getThrowable());
156157
assertTrue(callback.getThrowable() instanceof RuntimeException);
157158
// verify latch is released
158-
try {
159-
handler.getResult();
160-
fail();
161-
} catch (ExecutionException expected) {
162-
// expected
163-
}
159+
assertThrows(ExecutionException.class, new ThrowingRunnable() {
160+
@Override
161+
public void run() throws Throwable {
162+
handler.getResult();
163+
}
164+
});
164165
}
165166

166167
private static class AllGoodResponseConverter implements OAuthRequest.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
import static org.junit.Assert.assertNotNull;
55
import static org.junit.Assert.assertNull;
66
import static org.junit.Assert.assertTrue;
7-
import static org.junit.Assert.fail;
7+
import static org.junit.Assert.assertThrows;
88

99
import java.io.IOException;
1010
import java.util.concurrent.ExecutionException;
@@ -22,6 +22,7 @@
2222
import okhttp3.Protocol;
2323
import okhttp3.Request;
2424
import okhttp3.ResponseBody;
25+
import org.junit.function.ThrowingRunnable;
2526

2627
public class OAuthAsyncCompletionHandlerTest {
2728

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

9091
@Test
91-
public void shouldReleaseLatchOnIOException() throws Exception {
92+
public void shouldReleaseLatchOnIOException() {
9293
handler = new OAuthAsyncCompletionHandler<>(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() instanceof IOException);
107108
// verify latch is released
108-
try {
109-
future.get();
110-
fail();
111-
} catch (ExecutionException expected) {
112-
// expected
113-
}
109+
assertThrows(ExecutionException.class, new ThrowingRunnable() {
110+
@Override
111+
public void run() throws Throwable {
112+
future.get();
113+
}
114+
});
114115
}
115116

116117
@Test
117-
public void shouldReportOAuthException() throws Exception {
118+
public void shouldReportOAuthException() {
118119
handler = new OAuthAsyncCompletionHandler<>(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() instanceof OAuthException);
133134
// verify latch is released
134-
try {
135-
future.get();
136-
fail();
137-
} catch (ExecutionException expected) {
138-
// expected
139-
}
135+
assertThrows(ExecutionException.class, new ThrowingRunnable() {
136+
@Override
137+
public void run() throws Throwable {
138+
future.get();
139+
}
140+
});
140141
}
141142

142143
@Test
143-
public void shouldReleaseLatchOnCancel() throws Exception {
144+
public void shouldReleaseLatchOnCancel() {
144145
handler = new OAuthAsyncCompletionHandler<>(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() instanceof IOException);
151152
// verify latch is released
152-
try {
153-
future.get();
154-
fail();
155-
} catch (ExecutionException expected) {
156-
// expected
157-
}
153+
assertThrows(ExecutionException.class, new ThrowingRunnable() {
154+
@Override
155+
public void run() throws Throwable {
156+
future.get();
157+
}
158+
});
158159
}
159160

160161
@Test
161-
public void shouldReleaseLatchOnFailure() throws Exception {
162+
public void shouldReleaseLatchOnFailure() {
162163
handler = new OAuthAsyncCompletionHandler<>(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() instanceof IOException);
169170
// verify latch is released
170-
try {
171-
future.get();
172-
fail();
173-
} catch (ExecutionException expected) {
174-
// expected
175-
}
171+
assertThrows(ExecutionException.class, new ThrowingRunnable() {
172+
@Override
173+
public void run() throws Throwable {
174+
future.get();
175+
}
176+
});
176177
}
177178

178179
private static class AllGoodResponseConverter implements OAuthRequest.ResponseConverter<String> {

0 commit comments

Comments
 (0)