88import java .io .IOException ;
99
1010import org .baeldung .okhttp .ProgressRequestWrapper ;
11+ import org .junit .Before ;
1112import org .junit .Test ;
1213
1314import okhttp3 .Call ;
@@ -22,10 +23,18 @@ public class OkHttpFileUploadingLiveTest {
2223
2324 private static final String BASE_URL = "http://localhost:8080/spring-rest" ;
2425
26+ OkHttpClient client ;
27+
28+ @ Before
29+ public void init () {
30+
31+ client = new OkHttpClient ();
32+ }
33+
2534 @ Test
2635 public void whenUploadFile_thenCorrect () throws IOException {
2736
28- OkHttpClient client = new OkHttpClient ();
37+ client = new OkHttpClient ();
2938
3039 RequestBody requestBody = new MultipartBody .Builder ()
3140 .setType (MultipartBody .FORM )
@@ -47,22 +56,19 @@ public void whenUploadFile_thenCorrect() throws IOException {
4756 @ Test
4857 public void whenGetUploadFileProgress_thenCorrect () throws IOException {
4958
50- OkHttpClient client = new OkHttpClient ();
59+ client = new OkHttpClient ();
5160
5261 RequestBody requestBody = new MultipartBody .Builder ()
5362 .setType (MultipartBody .FORM )
5463 .addFormDataPart ("file" , "file.txt" ,
5564 RequestBody .create (MediaType .parse ("application/octet-stream" ), new File ("src/test/resources/test.txt" )))
5665 .build ();
5766
67+ ProgressRequestWrapper countingBody = new ProgressRequestWrapper (requestBody , (long bytesWritten , long contentLength ) -> {
5868
59- ProgressRequestWrapper .ProgressListener listener = (bytesWritten , contentLength ) -> {
60-
61- float percentage = 100f * bytesWritten / contentLength ;
69+ float percentage = 100f * bytesWritten / contentLength ;
6270 assertFalse (Float .compare (percentage , 100 ) > 0 );
63- };
64-
65- ProgressRequestWrapper countingBody = new ProgressRequestWrapper (requestBody , listener );
71+ });
6672
6773 Request request = new Request .Builder ()
6874 .url (BASE_URL + "/users/upload" )
0 commit comments