Skip to content

Commit 2892e93

Browse files
committed
Code improvement
1 parent 5329c54 commit 2892e93

2 files changed

Lines changed: 13 additions & 18 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import okhttp3.Request;
1515
import okhttp3.Response;
1616

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

1922
private static final String BASE_URL = "http://localhost:8080/spring-rest";

‎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)