Add method init for common logic

This commit is contained in:
Ivan Paolillo 2016-11-21 11:42:13 +01:00
parent 91faa251d5
commit 540075ca39
5 changed files with 67 additions and 25 deletions

View File

@ -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.ProgressListener listener = new ProgressRequestWrapper.ProgressListener() {
public void onRequestProgress(long bytesWritten, long contentLength) {
ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, (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")

View File

@ -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")

View File

@ -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();
}
}

View File

@ -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.

View File

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