You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2022/07/01 00:53:35 UTC

[servicecomb-java-chassis] branch master updated: [SCB-2475] migrate common-rest module to junit5 part 1 (#3168)

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

liubao 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 065a31ebf [SCB-2475] migrate common-rest module to junit5 part 1 (#3168)
065a31ebf is described below

commit 065a31ebff8173201632f85e7db8940e498516c4
Author: TingTing Wang <19...@qq.com>
AuthorDate: Fri Jul 1 08:53:30 2022 +0800

    [SCB-2475] migrate common-rest module to junit5 part 1 (#3168)
---
 .../servicecomb/common/rest/TestDefPath.java       |  2 +-
 .../common/rest/TestRestEngineSchemaListener.java  | 10 +--
 .../servicecomb/common/rest/TestUploadConfig.java  | 10 +--
 .../common/rest/codec/TestRestCodec.java           | 97 ++++++++++------------
 4 files changed, 53 insertions(+), 66 deletions(-)

diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestDefPath.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestDefPath.java
index 39aa648fc..e6b1f4bcf 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestDefPath.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestDefPath.java
@@ -19,7 +19,7 @@ package org.apache.servicecomb.common.rest;
 
 import org.apache.servicecomb.common.rest.definition.path.PathRegExp;
 import org.junit.jupiter.api.Assertions;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
 public class TestDefPath {
 
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestRestEngineSchemaListener.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestRestEngineSchemaListener.java
index d8df2e71c..d3cb3ebeb 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestRestEngineSchemaListener.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestRestEngineSchemaListener.java
@@ -25,17 +25,17 @@ import org.apache.servicecomb.core.SCBEngine;
 import org.apache.servicecomb.core.bootstrap.SCBBootstrap;
 import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
 
 public class TestRestEngineSchemaListener {
   static SCBEngine scbEngine;
 
   static ServicePathManager spm;
 
-  @BeforeClass
+  @BeforeAll
   public static void setup() {
     ConfigUtil.installDynamicConfig();
     scbEngine = SCBBootstrap.createSCBEngineForTest()
@@ -44,7 +44,7 @@ public class TestRestEngineSchemaListener {
     spm = ServicePathManager.getServicePathManager(scbEngine.getProducerMicroserviceMeta());
   }
 
-  @AfterClass
+  @AfterAll
   public static void teardown() {
     scbEngine.destroy();
     ArchaiusUtils.resetConfig();
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestUploadConfig.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestUploadConfig.java
index 4409b11ae..4081818d0 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestUploadConfig.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestUploadConfig.java
@@ -19,18 +19,18 @@ package org.apache.servicecomb.common.rest;
 import javax.servlet.MultipartConfigElement;
 
 import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
-import org.junit.After;
+import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 
 public class TestUploadConfig {
-  @Before
+  @BeforeEach
   public void setUp() {
     ArchaiusUtils.resetConfig();
   }
 
-  @After
+  @AfterEach
   public void tearDown() {
     ArchaiusUtils.resetConfig();
   }
diff --git a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java
index 41dbf4502..67d2c9147 100644
--- a/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java
+++ b/common/common-rest/src/test/java/org/apache/servicecomb/common/rest/codec/TestRestCodec.java
@@ -17,8 +17,6 @@
 
 package org.apache.servicecomb.common.rest.codec;
 
-import static org.mockito.Mockito.when;
-
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -31,18 +29,17 @@ import org.apache.servicecomb.common.rest.codec.param.ParamValueProcessor;
 import org.apache.servicecomb.common.rest.codec.param.RestClientRequestImpl;
 import org.apache.servicecomb.common.rest.definition.RestOperationMeta;
 import org.apache.servicecomb.common.rest.definition.RestParam;
+import org.apache.servicecomb.core.definition.OperationMeta;
 import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
 import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
-import org.junit.AfterClass;
+import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.Assertions;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
 import org.mockito.Mockito;
 
 import io.swagger.models.parameters.HeaderParameter;
 import io.swagger.models.parameters.Parameter;
-import mockit.Expectations;
-import mockit.Mocked;
 
 public class TestRestCodec {
 
@@ -58,23 +55,22 @@ public class TestRestCodec {
 
   private static List<RestParam> paramList = null;
 
-  @BeforeClass
+  @BeforeAll
   public static void beforeClass() {
     Parameter hp = new HeaderParameter();
     hp.setName("header");
     RestParam restParam = new RestParam(hp, int.class);
 
     restOperation = Mockito.mock(RestOperationMeta.class);
-    //        clientRequest = Mockito.mock(RestClientRequest.class);
     paramList = new ArrayList<>();
 
 
     paramList.add(restParam);
-    when(restOperation.getParamList()).thenReturn(paramList);
-    when(restOperation.getParamByName("test")).thenReturn(restParam);
+    Mockito.when(restOperation.getParamList()).thenReturn(paramList);
+    Mockito.when(restOperation.getParamByName("test")).thenReturn(restParam);
   }
 
-  @AfterClass
+  @AfterAll
   public static void afterClass() {
     restOperation = null;
     clientRequest = null;
@@ -95,50 +91,42 @@ public class TestRestCodec {
   }
 
   @Test
-  public void testRestToArgs(@Mocked HttpServletRequest request,
-      @Mocked RestOperationMeta restOperation, @Mocked RestParam restParam,
-      @Mocked ParamValueProcessor processer) throws Exception {
+  public void testRestToArgs() throws Exception {
+    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+    RestOperationMeta restOperation = Mockito.mock(RestOperationMeta.class);
+    RestParam restParam = Mockito.mock(RestParam.class);
+    ParamValueProcessor processor = Mockito.mock(ParamValueProcessor.class);
     List<RestParam> params = new ArrayList<>();
     params.add(restParam);
     String s = "my";
-
-    new Expectations() {
-      {
-        restOperation.getParamList();
-        result = params;
-        restParam.getParamProcessor();
-        result = processer;
-        processer.getValue(request);
-        result = s;
-        restParam.getParamName();
-        result = "test";
-      }
-    };
+    Mockito.when(restOperation.getParamList()).thenReturn(params);
+    Mockito.when(restParam.getParamProcessor()).thenReturn(processor);
+    Mockito.when(processor.getValue(request)).thenReturn(s);
+    Mockito.when(restParam.getParamName()).thenReturn("test");
 
     Map<String, Object> xx = RestCodec.restToArgs(request, restOperation);
     Assertions.assertEquals(xx.get("test"), s);
   }
 
   @Test
-  public void testRestToArgsExcetpion(@Mocked HttpServletRequest request,
-      @Mocked RestOperationMeta restOperation, @Mocked RestParam restParam,
-      @Mocked ParamValueProcessor processer) throws Exception {
+  public void testRestToArgsException() throws Exception {
+    ParamValueProcessor processor = Mockito.mock(ParamValueProcessor.class);
+    Mockito.when(processor.getValue(Mockito.any())).thenThrow(new Exception("bad request parame"));
+
+    RestOperationMeta restOperation = Mockito.mock(RestOperationMeta.class);
+    RestParam restParam = Mockito.mock(RestParam.class);
+    Mockito.when(restParam.getParamProcessor()).thenReturn(processor);
+
     List<RestParam> params = new ArrayList<>();
     params.add(restParam);
+    Mockito.when(restOperation.getParamList()).thenReturn(params);
 
-    new Expectations() {
-      {
-        restOperation.getParamList();
-        result = params;
-        restParam.getParamProcessor();
-        result = processer;
-        processer.getValue(request);
-        result = new Exception("bad request parame");
-      }
-    };
+    OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
+    Mockito.when(restOperation.getOperationMeta()).thenReturn(operationMeta);
 
     boolean success = false;
     try {
+      HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
       RestCodec.restToArgs(request, restOperation);
       success = true;
     } catch (InvocationException e) {
@@ -149,26 +137,25 @@ public class TestRestCodec {
   }
 
   @Test
-  public void testRestToArgsInstanceExcetpion(@Mocked HttpServletRequest request,
-      @Mocked RestOperationMeta restOperation, @Mocked RestParam restParam,
-      @Mocked ParamValueProcessor processer) throws Exception {
+  public void testRestToArgsInstanceException() throws Exception {
+    InvocationException exception = new InvocationException(Status.BAD_REQUEST, "Parameter is not valid.");
+    ParamValueProcessor processor = Mockito.mock(ParamValueProcessor.class);
+    Mockito.when(processor.getValue(Mockito.any())).thenThrow(exception);
+
+    RestOperationMeta restOperation = Mockito.mock(RestOperationMeta.class);
+    RestParam restParam = Mockito.mock(RestParam.class);
+    Mockito.when(restParam.getParamProcessor()).thenReturn(processor);
+
     List<RestParam> params = new ArrayList<>();
     params.add(restParam);
-    InvocationException exception = new InvocationException(Status.BAD_REQUEST, "Parameter is not valid.");
+    Mockito.when(restOperation.getParamList()).thenReturn(params);
 
-    new Expectations() {
-      {
-        restOperation.getParamList();
-        result = params;
-        restParam.getParamProcessor();
-        result = processer;
-        processer.getValue(request);
-        result = exception;
-      }
-    };
+    OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
+    Mockito.when(restOperation.getOperationMeta()).thenReturn(operationMeta);
 
     boolean success = false;
     try {
+      HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
       RestCodec.restToArgs(request, restOperation);
       success = true;
     } catch (InvocationException e) {