Skip to content

Commit 4530246

Browse files
authored
Merge pull request eugenp#811 from ivanp81/master
A Guide to OkHttp
2 parents d07908f + 91faa25 commit 4530246

2 files changed

Lines changed: 18 additions & 20 deletions

File tree

‎spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.hamcrest.Matchers.equalTo;
44
import static org.junit.Assert.assertThat;
5+
import static org.junit.Assert.fail;
56

67
import java.io.IOException;
78

@@ -14,6 +15,9 @@
1415
import okhttp3.Request;
1516
import okhttp3.Response;
1617

18+
//@RunWith(SpringJUnit4ClassRunner.class)
19+
//@WebAppConfiguration
20+
//@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/api-servlet.xml")
1721
public class OkHttpGetLiveTest {
1822

1923
private static final String BASE_URL = "http://localhost:8080/spring-rest";
@@ -54,7 +58,7 @@ public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException {
5458
}
5559

5660
@Test
57-
public void whenAsynchronousGetRequest_thenCorrect() {
61+
public void whenAsynchronousGetRequest_thenCorrect() throws InterruptedException {
5862

5963
OkHttpClient client = new OkHttpClient();
6064

@@ -71,8 +75,10 @@ public void onResponse(Call call, Response response) throws IOException {
7175
}
7276

7377
public void onFailure(Call call, IOException e) {
74-
78+
fail();
7579
}
7680
});
81+
82+
Thread.sleep(3000);
7783
}
7884
}

‎spring-rest/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java‎

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void whenSetRequestTimeout_thenFail() throws IOException {
3737
response.close();
3838
}
3939

40-
@Test
40+
@Test(expected = IOException.class)
4141
public void whenCancelRequest_thenCorrect() throws IOException {
4242

4343
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
@@ -49,30 +49,22 @@ public void whenCancelRequest_thenCorrect() throws IOException {
4949
.build();
5050

5151
final int seconds = 1;
52-
5352
final long startNanos = System.nanoTime();
53+
5454
final Call call = client.newCall(request);
5555

5656
// Schedule a job to cancel the call in 1 second.
57-
executor.schedule(new Runnable() {
58-
public void run() {
59-
60-
logger.debug("Canceling call: " + (System.nanoTime() - startNanos) / 1e9f);
61-
call.cancel();
62-
logger.debug("Canceled call: " + (System.nanoTime() - startNanos) / 1e9f);
63-
}
64-
}, seconds, TimeUnit.SECONDS);
65-
66-
try {
57+
executor.schedule(() -> {
6758

68-
logger.debug("Executing call: " + (System.nanoTime() - startNanos) / 1e9f);
69-
Response response = call.execute();
70-
logger.debug("Call was expected to fail, but completed: " + (System.nanoTime() - startNanos) / 1e9f, response);
59+
logger.debug("Canceling call: " + (System.nanoTime() - startNanos) / 1e9f);
60+
call.cancel();
61+
logger.debug("Canceled call: " + (System.nanoTime() - startNanos) / 1e9f);
7162

72-
} catch (IOException e) {
63+
}, seconds, TimeUnit.SECONDS);
7364

74-
logger.debug("Call failed as expected: " + (System.nanoTime() - startNanos) / 1e9f, e);
75-
}
65+
logger.debug("Executing call: " + (System.nanoTime() - startNanos) / 1e9f);
66+
Response response = call.execute();
67+
logger.debug("Call completed: " + (System.nanoTime() - startNanos) / 1e9f, response);
7668
}
7769

7870
@Test

0 commit comments

Comments
 (0)