From 540075ca390a859a1f1bff4966095b3b522dd74b Mon Sep 17 00:00:00 2001 From: Ivan Paolillo Date: Mon, 21 Nov 2016 11:42:13 +0100 Subject: [PATCH 1/3] Add method init for common logic --- .../okhttp/OkHttpFileUploadingLiveTest.java | 26 +++++++++++-------- .../baeldung/okhttp/OkHttpGetLiveTest.java | 25 +++++++++++++----- .../baeldung/okhttp/OkHttpHeaderLiveTest.java | 13 +++++++--- .../baeldung/okhttp/OkHttpMiscLiveTest.java | 11 +++++++- .../okhttp/OkHttpPostingLiveTest.java | 17 +++++++++--- 5 files changed, 67 insertions(+), 25 deletions(-) diff --git a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java index d1cc67b99a..d7aff8207a 100644 --- a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java @@ -8,6 +8,7 @@ import java.io.File; import java.io.IOException; import org.baeldung.okhttp.ProgressRequestWrapper; +import org.junit.Before; import org.junit.Test; import okhttp3.Call; @@ -22,10 +23,18 @@ public class OkHttpFileUploadingLiveTest { private static final String BASE_URL = "http://localhost:8080/spring-rest"; + OkHttpClient client; + + @Before + public void init() { + + client = new OkHttpClient(); + } + @Test public void whenUploadFile_thenCorrect() throws IOException { - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -47,7 +56,7 @@ public class OkHttpFileUploadingLiveTest { @Test public void whenGetUploadFileProgress_thenCorrect() throws IOException { - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) @@ -55,17 +64,12 @@ public class OkHttpFileUploadingLiveTest { RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))) .build(); + ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, (long bytesWritten, long contentLength) -> { - ProgressRequestWrapper.ProgressListener listener = new ProgressRequestWrapper.ProgressListener() { + float percentage = 100f * bytesWritten / contentLength; + assertFalse(Float.compare(percentage, 100) > 0); - public void onRequestProgress(long bytesWritten, long contentLength) { - - float percentage = 100f * bytesWritten / contentLength; - assertFalse(Float.compare(percentage, 100) > 0); - } - }; - - ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, listener); + }); Request request = new Request.Builder() .url(BASE_URL + "/users/upload") diff --git a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java index 632d7577a4..19bfd6d46b 100644 --- a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java @@ -6,7 +6,12 @@ import static org.junit.Assert.fail; import java.io.IOException; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; import okhttp3.Call; import okhttp3.Callback; @@ -15,17 +20,25 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -//@RunWith(SpringJUnit4ClassRunner.class) -//@WebAppConfiguration -//@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/api-servlet.xml") +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/api-servlet.xml") public class OkHttpGetLiveTest { private static final String BASE_URL = "http://localhost:8080/spring-rest"; + OkHttpClient client; + + @Before + public void init() { + + client = new OkHttpClient(); + } + @Test public void whenGetRequest_thenCorrect() throws IOException { - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); Request request = new Request.Builder() .url(BASE_URL + "/date") @@ -40,7 +53,7 @@ public class OkHttpGetLiveTest { @Test public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException { - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder(); urlBuilder.addQueryParameter("id", "1"); @@ -60,7 +73,7 @@ public class OkHttpGetLiveTest { @Test public void whenAsynchronousGetRequest_thenCorrect() throws InterruptedException { - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); Request request = new Request.Builder() .url(BASE_URL + "/date") diff --git a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java index 8eddfcb135..1b73565aa4 100644 --- a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java @@ -2,6 +2,7 @@ package org.baeldung.okhttp; import java.io.IOException; +import org.junit.Before; import org.junit.Test; import okhttp3.Call; @@ -13,10 +14,18 @@ public class OkHttpHeaderLiveTest { private static final String SAMPLE_URL = "http://www.github.com"; + OkHttpClient client; + + @Before + public void init() { + + client = new OkHttpClient(); + } + @Test public void whenSetHeader_thenCorrect() throws IOException { - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); Request request = new Request.Builder() .url(SAMPLE_URL) @@ -43,6 +52,4 @@ public class OkHttpHeaderLiveTest { Response response = call.execute(); response.close(); } - - } diff --git a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java index dcdb4e328c..39f66a8024 100644 --- a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java @@ -6,6 +6,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,6 +22,14 @@ public class OkHttpMiscLiveTest { private static final String BASE_URL = "http://localhost:8080/spring-rest"; private static Logger logger = LoggerFactory.getLogger(OkHttpMiscLiveTest.class); + OkHttpClient client; + + @Before + public void init() { + + client = new OkHttpClient(); + } + @Test public void whenSetRequestTimeout_thenFail() throws IOException { @@ -42,7 +51,7 @@ public class OkHttpMiscLiveTest { ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); Request request = new Request.Builder() .url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. diff --git a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java index 18bd4cdcb3..59844d547a 100644 --- a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java @@ -6,6 +6,7 @@ import static org.junit.Assert.assertThat; import java.io.File; import java.io.IOException; +import org.junit.Before; import org.junit.Test; import okhttp3.Call; @@ -23,10 +24,18 @@ public class OkHttpPostingLiveTest { private static final String BASE_URL = "http://localhost:8080/spring-rest"; private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; + OkHttpClient client; + + @Before + public void init() { + + client = new OkHttpClient(); + } + @Test public void whenSendPostRequest_thenCorrect() throws IOException { - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); RequestBody formBody = new FormBody.Builder() .add("username", "test") @@ -49,7 +58,7 @@ public class OkHttpPostingLiveTest { String postBody = "test post"; - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); Request request = new Request.Builder() .url(URL_SECURED_BY_BASIC_AUTHENTICATION) @@ -66,7 +75,7 @@ public class OkHttpPostingLiveTest { @Test public void whenPostJson_thenCorrect() throws IOException { - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); String json = "{\"id\":1,\"name\":\"John\"}"; @@ -86,7 +95,7 @@ public class OkHttpPostingLiveTest { @Test public void whenSendMultipartRequest_thenCorrect() throws IOException { - OkHttpClient client = new OkHttpClient(); + client = new OkHttpClient(); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) From e66e9d445f301cdccd370e721eea1e9ab92cc131 Mon Sep 17 00:00:00 2001 From: Ivan Paolillo Date: Mon, 21 Nov 2016 11:43:32 +0100 Subject: [PATCH 2/3] Code improvement --- .../src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java index 19bfd6d46b..7a5f598569 100644 --- a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java @@ -20,9 +20,6 @@ import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; -@RunWith(SpringJUnit4ClassRunner.class) -@WebAppConfiguration -@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/api-servlet.xml") public class OkHttpGetLiveTest { private static final String BASE_URL = "http://localhost:8080/spring-rest"; From 4d4afa6ca454b0172260e8cc210e7215442746c3 Mon Sep 17 00:00:00 2001 From: Ivan Paolillo Date: Mon, 21 Nov 2016 13:53:37 +0100 Subject: [PATCH 3/3] Code improvement --- .../src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java index 7a5f598569..f077efce0d 100644 --- a/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java @@ -8,10 +8,6 @@ import java.io.IOException; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; import okhttp3.Call; import okhttp3.Callback;