You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by sh...@apache.org on 2022/06/29 03:58:13 UTC

[servicecomb-java-chassis] branch master updated: [SCB-2615-part2] migrate foundation-vertx module to junit5 (#3152)

This is an automated email from the ASF dual-hosted git repository.

shoothzj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 21b89b655 [SCB-2615-part2] migrate foundation-vertx module to junit5 (#3152)
21b89b655 is described below

commit 21b89b655c4aa6633ff5bc043048d21a6d71ce88
Author: TingTing Wang <19...@qq.com>
AuthorDate: Wed Jun 29 11:58:09 2022 +0800

    [SCB-2615-part2] migrate foundation-vertx module to junit5 (#3152)
---
 .../foundation/vertx/http/TestFileUploadPart.java  | 64 ++++++-----------
 ...TestVertxClientRequestToHttpServletRequest.java | 82 ++++++----------------
 ...stVertxClientResponseToHttpServletResponse.java | 62 +++++-----------
 3 files changed, 59 insertions(+), 149 deletions(-)

diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestFileUploadPart.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestFileUploadPart.java
index bd436c197..b36b6865b 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestFileUploadPart.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestFileUploadPart.java
@@ -25,17 +25,17 @@ import java.util.UUID;
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
 
 import io.vertx.ext.web.FileUpload;
-import mockit.Expectations;
-import mockit.Mocked;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 
 public class TestFileUploadPart {
-  @Mocked
+
   FileUpload fileUpload;
 
   FileUploadPart part;
@@ -44,26 +44,27 @@ public class TestFileUploadPart {
 
   static String content = "fileContent";
 
-  @BeforeClass
+  @BeforeAll
   public static void classSetup() throws IOException {
     file = File.createTempFile("upload", ".txt");
     file.deleteOnExit();
     FileUtils.writeStringToFile(file, content, StandardCharsets.UTF_8, false);
   }
 
-  @Before
+  @BeforeEach
   public void setup() {
+    fileUpload = Mockito.mock(FileUpload.class);
     part = new FileUploadPart(fileUpload);
   }
 
+  @AfterEach
+  public void after() {
+    Mockito.reset(fileUpload);
+  }
+
   @Test
   public void getInputStream() throws IOException {
-    new Expectations() {
-      {
-        fileUpload.uploadedFileName();
-        result = file.getAbsolutePath();
-      }
-    };
+    Mockito.when(fileUpload.uploadedFileName()).thenReturn(file.getAbsolutePath());
     try (InputStream is = part.getInputStream()) {
       Assertions.assertEquals(content, IOUtils.toString(is, StandardCharsets.UTF_8));
     }
@@ -72,12 +73,7 @@ public class TestFileUploadPart {
   @Test
   public void getContentType() {
     String contentType = "type";
-    new Expectations() {
-      {
-        fileUpload.contentType();
-        result = contentType;
-      }
-    };
+    Mockito.when(fileUpload.contentType()).thenReturn(contentType);
 
     Assertions.assertEquals(contentType, part.getContentType());
   }
@@ -85,12 +81,7 @@ public class TestFileUploadPart {
   @Test
   public void getName() {
     String name = "pName";
-    new Expectations() {
-      {
-        fileUpload.name();
-        result = name;
-      }
-    };
+    Mockito.when(fileUpload.name()).thenReturn(name);
 
     Assertions.assertEquals(name, part.getName());
   }
@@ -98,12 +89,7 @@ public class TestFileUploadPart {
   @Test
   public void getSubmittedFileName() {
     String clientName = "clientName";
-    new Expectations() {
-      {
-        fileUpload.fileName();
-        result = clientName;
-      }
-    };
+    Mockito.when(fileUpload.fileName()).thenReturn(clientName);
 
     Assertions.assertEquals(clientName, part.getSubmittedFileName());
   }
@@ -111,24 +97,14 @@ public class TestFileUploadPart {
   @Test
   public void getSize() {
     long fileSize = 10;
-    new Expectations() {
-      {
-        fileUpload.size();
-        result = fileSize;
-      }
-    };
+    Mockito.when(fileUpload.size()).thenReturn(fileSize);
 
     Assertions.assertEquals(fileSize, part.getSize());
   }
 
   @Test
   public void write() throws IOException {
-    new Expectations() {
-      {
-        fileUpload.uploadedFileName();
-        result = file.getAbsolutePath();
-      }
-    };
+    Mockito.when(fileUpload.uploadedFileName()).thenReturn(file.getAbsolutePath());
 
     File targetFile = new File(UUID.randomUUID().toString());
     targetFile.deleteOnExit();
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientRequestToHttpServletRequest.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientRequestToHttpServletRequest.java
index 2633a764c..881c1b6de 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientRequestToHttpServletRequest.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientRequestToHttpServletRequest.java
@@ -24,50 +24,46 @@ import javax.ws.rs.core.HttpHeaders;
 import org.apache.servicecomb.foundation.common.http.HttpUtils;
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
-import org.junit.Before;
-import org.junit.Test;
 
 import io.vertx.core.MultiMap;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpClientRequest;
 import io.vertx.core.http.HttpMethod;
 import mockit.Expectations;
-import mockit.Mocked;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 
 public class TestVertxClientRequestToHttpServletRequest {
-  @Mocked
   HttpClientRequest clientRequest;
 
   Buffer bodyBuffer = Buffer.buffer();
 
   VertxClientRequestToHttpServletRequest request;
 
-  @Before
+  @BeforeEach
   public void setup() {
+    clientRequest = Mockito.mock(HttpClientRequest.class);
     request = new VertxClientRequestToHttpServletRequest(clientRequest, bodyBuffer);
   }
 
+  @AfterEach
+  public void after() {
+    Mockito.reset(clientRequest);
+  }
+
   @Test
   public void testGetRequestURI() {
-    new Expectations() {
-      {
-        clientRequest.path();
-        result = "/path";
-      }
-    };
+    Mockito.when(clientRequest.path()).thenReturn("/path");
 
     Assertions.assertEquals("/path", request.getRequestURI());
   }
 
   @Test
   public void testGetQueryString() {
-    new Expectations() {
-      {
-        clientRequest.query();
-        result = "a=1&b=2";
-      }
-    };
+    Mockito.when(clientRequest.query()).thenReturn("a=1&b=2");
 
     Assertions.assertEquals("a=1&b=2", request.getQueryString());
   }
@@ -76,12 +72,7 @@ public class TestVertxClientRequestToHttpServletRequest {
   public void testGetHeader() {
     MultiMap headers = MultiMap.caseInsensitiveMultiMap();
     headers.add("name", "value");
-    new Expectations() {
-      {
-        clientRequest.headers();
-        result = headers;
-      }
-    };
+    Mockito.when(clientRequest.headers()).thenReturn(headers);
 
     Assertions.assertEquals("value", request.getHeader("name"));
   }
@@ -90,12 +81,7 @@ public class TestVertxClientRequestToHttpServletRequest {
   public void testGetHeaders() {
     MultiMap headers = MultiMap.caseInsensitiveMultiMap();
     headers.add("name", "value");
-    new Expectations() {
-      {
-        clientRequest.headers();
-        result = headers;
-      }
-    };
+    Mockito.when(clientRequest.headers()).thenReturn(headers);
 
     MatcherAssert.assertThat(Collections.list(request.getHeaders("name")), Matchers.contains("value"));
   }
@@ -104,12 +90,7 @@ public class TestVertxClientRequestToHttpServletRequest {
   public void testGetHeaderNames() {
     MultiMap headers = MultiMap.caseInsensitiveMultiMap();
     headers.add("name", "value");
-    new Expectations() {
-      {
-        clientRequest.headers();
-        result = headers;
-      }
-    };
+    Mockito.when(clientRequest.headers()).thenReturn(headers);
 
     MatcherAssert.assertThat(Collections.list(request.getHeaderNames()), Matchers.contains("name"));
   }
@@ -117,12 +98,7 @@ public class TestVertxClientRequestToHttpServletRequest {
   @Test
   public void testSetHeader() {
     MultiMap headers = MultiMap.caseInsensitiveMultiMap();
-    new Expectations() {
-      {
-        clientRequest.headers();
-        result = headers;
-      }
-    };
+    Mockito.when(clientRequest.headers()).thenReturn(headers);
 
     request.setHeader("name", "v1");
     request.setHeader("name", "v2");
@@ -132,12 +108,7 @@ public class TestVertxClientRequestToHttpServletRequest {
   @Test
   public void testAddHeader() {
     MultiMap headers = MultiMap.caseInsensitiveMultiMap();
-    new Expectations() {
-      {
-        clientRequest.headers();
-        result = headers;
-      }
-    };
+    Mockito.when(clientRequest.headers()).thenReturn(headers);
 
     request.addHeader("name", "v1");
     request.addHeader("name", "v2");
@@ -151,12 +122,7 @@ public class TestVertxClientRequestToHttpServletRequest {
 
   @Test
   public void getMethod() {
-    new Expectations() {
-      {
-        clientRequest.getMethod();
-        result = HttpMethod.GET;
-      }
-    };
+    Mockito.when(clientRequest.getMethod()).thenReturn(HttpMethod.GET);
 
     Assertions.assertEquals("GET", request.getMethod());
   }
@@ -164,12 +130,7 @@ public class TestVertxClientRequestToHttpServletRequest {
   @Test
   public void getContentType() {
     MultiMap headers = MultiMap.caseInsensitiveMultiMap();
-    new Expectations() {
-      {
-        clientRequest.headers();
-        result = headers;
-      }
-    };
+    Mockito.when(clientRequest.headers()).thenReturn(headers);
 
     request.addHeader(HttpHeaders.CONTENT_TYPE, "ct");
 
@@ -186,10 +147,9 @@ public class TestVertxClientRequestToHttpServletRequest {
       {
         HttpUtils.getCharsetFromContentType(contentType);
         result = characterEncoding;
-        clientRequest.headers();
-        result = headers;
       }
     };
+    Mockito.when(clientRequest.headers()).thenReturn(headers);
 
     request.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
 
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientResponseToHttpServletResponse.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientResponseToHttpServletResponse.java
index d8009211e..1969851f7 100644
--- a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientResponseToHttpServletResponse.java
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/http/TestVertxClientResponseToHttpServletResponse.java
@@ -22,51 +22,45 @@ import javax.ws.rs.core.Response.StatusType;
 
 import org.hamcrest.MatcherAssert;
 import org.hamcrest.Matchers;
-import org.junit.Before;
-import org.junit.Test;
 
 import io.vertx.core.MultiMap;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpClientResponse;
-import mockit.Expectations;
-import mockit.Mocked;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 
 public class TestVertxClientResponseToHttpServletResponse {
-  @Mocked
   HttpClientResponse clientResponse;
 
   Buffer bodyBuffer = Buffer.buffer();
 
   VertxClientResponseToHttpServletResponse response;
 
-  @Before
+  @BeforeEach
   public void setup() {
+    clientResponse = Mockito.mock(HttpClientResponse.class);
     response = new VertxClientResponseToHttpServletResponse(clientResponse, bodyBuffer);
   }
 
+  @AfterEach
+  public void after() {
+    Mockito.reset(clientResponse);
+  }
+
   @Test
   public void getStatus() {
-    new Expectations() {
-      {
-        clientResponse.statusCode();
-        result = 123;
-      }
-    };
+    Mockito.when(clientResponse.statusCode()).thenReturn(123);
 
     Assertions.assertEquals(123, response.getStatus());
   }
 
   @Test
   public void getStatusType() {
-    new Expectations() {
-      {
-        clientResponse.statusCode();
-        result = 123;
-        clientResponse.statusMessage();
-        result = "test";
-      }
-    };
+    Mockito.when(clientResponse.statusCode()).thenReturn(123);
+    Mockito.when(clientResponse.statusMessage()).thenReturn("test");
 
     StatusType type = response.getStatusType();
     Assertions.assertSame(type, response.getStatusType());
@@ -76,24 +70,14 @@ public class TestVertxClientResponseToHttpServletResponse {
 
   @Test
   public void getContentType() {
-    new Expectations() {
-      {
-        clientResponse.getHeader(HttpHeaders.CONTENT_TYPE);
-        result = "json";
-      }
-    };
+    Mockito.when(clientResponse.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("json");
 
     Assertions.assertEquals("json", response.getContentType());
   }
 
   @Test
   public void getHeader() {
-    new Expectations() {
-      {
-        clientResponse.getHeader("name");
-        result = "value";
-      }
-    };
+    Mockito.when(clientResponse.getHeader("name")).thenReturn("value");
 
     Assertions.assertEquals("value", response.getHeader("name"));
   }
@@ -103,12 +87,7 @@ public class TestVertxClientResponseToHttpServletResponse {
     MultiMap headers = MultiMap.caseInsensitiveMultiMap();
     headers.add("name", "v1");
     headers.add("name", "v2");
-    new Expectations() {
-      {
-        clientResponse.headers();
-        result = headers;
-      }
-    };
+    Mockito.when(clientResponse.headers()).thenReturn(headers);
 
     MatcherAssert.assertThat(response.getHeaders("name"), Matchers.contains("v1", "v2"));
   }
@@ -118,12 +97,7 @@ public class TestVertxClientResponseToHttpServletResponse {
     MultiMap headers = MultiMap.caseInsensitiveMultiMap();
     headers.add("n1", "v1");
     headers.add("n2", "v2");
-    new Expectations() {
-      {
-        clientResponse.headers();
-        result = headers;
-      }
-    };
+    Mockito.when(clientResponse.headers()).thenReturn(headers);
 
     MatcherAssert.assertThat(response.getHeaderNames(), Matchers.contains("n1", "n2"));
   }