You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2022/07/13 10:29:01 UTC

[GitHub] [servicecomb-java-chassis] Shoothzj commented on a diff in pull request #3186: [SCB-2636]migrate common-rest module to mockito part 1

Shoothzj commented on code in PR #3186:
URL: https://github.com/apache/servicecomb-java-chassis/pull/3186#discussion_r919917641


##########
common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java:
##########
@@ -96,89 +102,101 @@ public static void afterClass() {
   public void setUp() {
     creator = new RestVertxProducerInvocationCreator(routingContext, microserviceMeta, endpoint,
         requestEx, responseEx);
-  }
-
-  private void mockGetServicePathManager() {
-    mockGetServicePathManager(servicePathManager);
-  }
-
-  private void mockGetServicePathManager(final ServicePathManager servicePathManager) {
-    new Expectations(ServicePathManager.class) {
-      {
-        ServicePathManager.getServicePathManager(microserviceMeta);
-        result = servicePathManager;
-      }
-    };
+    creator = Mockito.spy(creator);
   }
 
   @Test
   public void should_failed_when_not_defined_any_schema() {
-    mockGetServicePathManager(null);
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(null)).thenReturn(servicePathManager);
 
-    InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
-    CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
+      InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());

Review Comment:
   we use two spaces instead of four spaces



##########
common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java:
##########
@@ -96,89 +102,101 @@ public static void afterClass() {
   public void setUp() {
     creator = new RestVertxProducerInvocationCreator(routingContext, microserviceMeta, endpoint,
         requestEx, responseEx);
-  }
-
-  private void mockGetServicePathManager() {
-    mockGetServicePathManager(servicePathManager);
-  }
-
-  private void mockGetServicePathManager(final ServicePathManager servicePathManager) {
-    new Expectations(ServicePathManager.class) {
-      {
-        ServicePathManager.getServicePathManager(microserviceMeta);
-        result = servicePathManager;
-      }
-    };
+    creator = Mockito.spy(creator);
   }
 
   @Test
   public void should_failed_when_not_defined_any_schema() {
-    mockGetServicePathManager(null);
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(null)).thenReturn(servicePathManager);
 
-    InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
-    CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
+      InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
+      CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
 
-    assertThat(throwable.getStatusCode()).isEqualTo(NOT_FOUND.getStatusCode());
-    assertThat(Json.encode(data)).isEqualTo("{\"code\":\"SCB.00000002\",\"message\":\"Not Found\"}");
+      assertThat(throwable.getStatusCode()).isEqualTo(NOT_FOUND.getStatusCode());
+      assertThat(Json.encode(data)).isEqualTo("{\"code\":\"SCB.00000002\",\"message\":\"Not Found\"}");
+    }
   }
 
   @Test
   public void should_failed_when_accept_is_not_support() {
-    mockGetServicePathManager();
-    new Expectations() {
-      {
-        requestEx.getHeader(HttpHeaders.ACCEPT);
-        result = "test-type";
-
-        restOperationMeta.ensureFindProduceProcessor(requestEx);
-        result = null;
-      }
-    };
-
-    InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
-    CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
-
-    assertThat(throwable.getStatusCode()).isEqualTo(NOT_ACCEPTABLE.getStatusCode());
-    assertThat(Json.encode(data))
-        .isEqualTo("{\"code\":\"SCB.00000000\",\"message\":\"Accept test-type is not supported\"}");
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(microserviceMeta)).thenReturn(servicePathManager);
+      Mockito.when(requestEx.getHeader(HttpHeaders.ACCEPT)).thenReturn("test-type");
+      Mockito.when(restOperationMeta.ensureFindProduceProcessor(requestEx)).thenReturn(null);
+      Mockito.when(creator.locateOperation(microserviceMeta)).thenReturn(locator);
+      Mockito.when(locator.getOperation()).thenReturn(restOperationMeta);
+      Mockito.when(restOperationMeta.getOperationMeta()).thenReturn(operationMeta);
+      Mockito.when(operationMeta.buildBaseProviderRuntimeType()).thenReturn(invocationRuntimeType);
+      Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
+      Mockito.when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
+      Mockito.when(microserviceMeta.getHandlerChain()).thenReturn(new ArrayList<>());
+
+      InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
+      CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
+
+      assertThat(throwable.getStatusCode()).isEqualTo(NOT_ACCEPTABLE.getStatusCode());

Review Comment:
   can we use `Assert.assertEquals`?



##########
common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java:
##########
@@ -96,89 +102,101 @@ public static void afterClass() {
   public void setUp() {
     creator = new RestVertxProducerInvocationCreator(routingContext, microserviceMeta, endpoint,
         requestEx, responseEx);
-  }
-
-  private void mockGetServicePathManager() {
-    mockGetServicePathManager(servicePathManager);
-  }
-
-  private void mockGetServicePathManager(final ServicePathManager servicePathManager) {
-    new Expectations(ServicePathManager.class) {
-      {
-        ServicePathManager.getServicePathManager(microserviceMeta);
-        result = servicePathManager;
-      }
-    };
+    creator = Mockito.spy(creator);
   }
 
   @Test
   public void should_failed_when_not_defined_any_schema() {
-    mockGetServicePathManager(null);
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(null)).thenReturn(servicePathManager);
 
-    InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
-    CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
+      InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
+      CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
 
-    assertThat(throwable.getStatusCode()).isEqualTo(NOT_FOUND.getStatusCode());
-    assertThat(Json.encode(data)).isEqualTo("{\"code\":\"SCB.00000002\",\"message\":\"Not Found\"}");
+      assertThat(throwable.getStatusCode()).isEqualTo(NOT_FOUND.getStatusCode());

Review Comment:
   can we use `Assert.assertEquals`?



##########
common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java:
##########
@@ -96,89 +102,101 @@ public static void afterClass() {
   public void setUp() {
     creator = new RestVertxProducerInvocationCreator(routingContext, microserviceMeta, endpoint,
         requestEx, responseEx);
-  }
-
-  private void mockGetServicePathManager() {
-    mockGetServicePathManager(servicePathManager);
-  }
-
-  private void mockGetServicePathManager(final ServicePathManager servicePathManager) {
-    new Expectations(ServicePathManager.class) {
-      {
-        ServicePathManager.getServicePathManager(microserviceMeta);
-        result = servicePathManager;
-      }
-    };
+    creator = Mockito.spy(creator);
   }
 
   @Test
   public void should_failed_when_not_defined_any_schema() {
-    mockGetServicePathManager(null);
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(null)).thenReturn(servicePathManager);
 
-    InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
-    CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
+      InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
+      CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
 
-    assertThat(throwable.getStatusCode()).isEqualTo(NOT_FOUND.getStatusCode());
-    assertThat(Json.encode(data)).isEqualTo("{\"code\":\"SCB.00000002\",\"message\":\"Not Found\"}");
+      assertThat(throwable.getStatusCode()).isEqualTo(NOT_FOUND.getStatusCode());
+      assertThat(Json.encode(data)).isEqualTo("{\"code\":\"SCB.00000002\",\"message\":\"Not Found\"}");
+    }
   }
 
   @Test
   public void should_failed_when_accept_is_not_support() {
-    mockGetServicePathManager();
-    new Expectations() {
-      {
-        requestEx.getHeader(HttpHeaders.ACCEPT);
-        result = "test-type";
-
-        restOperationMeta.ensureFindProduceProcessor(requestEx);
-        result = null;
-      }
-    };
-
-    InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
-    CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
-
-    assertThat(throwable.getStatusCode()).isEqualTo(NOT_ACCEPTABLE.getStatusCode());
-    assertThat(Json.encode(data))
-        .isEqualTo("{\"code\":\"SCB.00000000\",\"message\":\"Accept test-type is not supported\"}");
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(microserviceMeta)).thenReturn(servicePathManager);
+      Mockito.when(requestEx.getHeader(HttpHeaders.ACCEPT)).thenReturn("test-type");
+      Mockito.when(restOperationMeta.ensureFindProduceProcessor(requestEx)).thenReturn(null);
+      Mockito.when(creator.locateOperation(microserviceMeta)).thenReturn(locator);
+      Mockito.when(locator.getOperation()).thenReturn(restOperationMeta);
+      Mockito.when(restOperationMeta.getOperationMeta()).thenReturn(operationMeta);
+      Mockito.when(operationMeta.buildBaseProviderRuntimeType()).thenReturn(invocationRuntimeType);
+      Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
+      Mockito.when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
+      Mockito.when(microserviceMeta.getHandlerChain()).thenReturn(new ArrayList<>());
+
+      InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
+      CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
+
+      assertThat(throwable.getStatusCode()).isEqualTo(NOT_ACCEPTABLE.getStatusCode());
+      assertThat(Json.encode(data))
+              .isEqualTo("{\"code\":\"SCB.00000000\",\"message\":\"Accept test-type is not supported\"}");
+    }
   }
 
   @Test
   public void should_save_requestEx_in_invocation_context() {
-    mockGetServicePathManager();
-
-    Invocation invocation = creator.createAsync().join();
-
-    Object request = invocation.getLocalContext(RestConst.REST_REQUEST);
-    assertThat(request).isSameAs(requestEx);
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(microserviceMeta)).thenReturn(servicePathManager);
+      Mockito.when(creator.locateOperation(microserviceMeta)).thenReturn(locator);
+      Mockito.when(locator.getOperation()).thenReturn(restOperationMeta);
+      Mockito.when(restOperationMeta.getOperationMeta()).thenReturn(operationMeta);
+      Mockito.when(operationMeta.buildBaseProviderRuntimeType()).thenReturn(invocationRuntimeType);
+      Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
+      Mockito.when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
+      Mockito.when(microserviceMeta.getHandlerChain()).thenReturn(new ArrayList<>());
+      Mockito.doNothing().when(creator).initProduceProcessor();
+
+      Invocation invocation = creator.createAsync().join();
+
+      Object request = invocation.getLocalContext(RestConst.REST_REQUEST);
+      assertThat(request).isSameAs(requestEx);

Review Comment:
   can we use `Assert.assertEquals`?



##########
common/common-rest/src/test/java/org/apache/servicecomb/common/rest/RestProducerInvocationCreatorTest.java:
##########
@@ -96,89 +102,101 @@ public static void afterClass() {
   public void setUp() {
     creator = new RestVertxProducerInvocationCreator(routingContext, microserviceMeta, endpoint,
         requestEx, responseEx);
-  }
-
-  private void mockGetServicePathManager() {
-    mockGetServicePathManager(servicePathManager);
-  }
-
-  private void mockGetServicePathManager(final ServicePathManager servicePathManager) {
-    new Expectations(ServicePathManager.class) {
-      {
-        ServicePathManager.getServicePathManager(microserviceMeta);
-        result = servicePathManager;
-      }
-    };
+    creator = Mockito.spy(creator);
   }
 
   @Test
   public void should_failed_when_not_defined_any_schema() {
-    mockGetServicePathManager(null);
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(null)).thenReturn(servicePathManager);
 
-    InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
-    CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
+      InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
+      CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
 
-    assertThat(throwable.getStatusCode()).isEqualTo(NOT_FOUND.getStatusCode());
-    assertThat(Json.encode(data)).isEqualTo("{\"code\":\"SCB.00000002\",\"message\":\"Not Found\"}");
+      assertThat(throwable.getStatusCode()).isEqualTo(NOT_FOUND.getStatusCode());
+      assertThat(Json.encode(data)).isEqualTo("{\"code\":\"SCB.00000002\",\"message\":\"Not Found\"}");
+    }
   }
 
   @Test
   public void should_failed_when_accept_is_not_support() {
-    mockGetServicePathManager();
-    new Expectations() {
-      {
-        requestEx.getHeader(HttpHeaders.ACCEPT);
-        result = "test-type";
-
-        restOperationMeta.ensureFindProduceProcessor(requestEx);
-        result = null;
-      }
-    };
-
-    InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
-    CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
-
-    assertThat(throwable.getStatusCode()).isEqualTo(NOT_ACCEPTABLE.getStatusCode());
-    assertThat(Json.encode(data))
-        .isEqualTo("{\"code\":\"SCB.00000000\",\"message\":\"Accept test-type is not supported\"}");
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(microserviceMeta)).thenReturn(servicePathManager);
+      Mockito.when(requestEx.getHeader(HttpHeaders.ACCEPT)).thenReturn("test-type");
+      Mockito.when(restOperationMeta.ensureFindProduceProcessor(requestEx)).thenReturn(null);
+      Mockito.when(creator.locateOperation(microserviceMeta)).thenReturn(locator);
+      Mockito.when(locator.getOperation()).thenReturn(restOperationMeta);
+      Mockito.when(restOperationMeta.getOperationMeta()).thenReturn(operationMeta);
+      Mockito.when(operationMeta.buildBaseProviderRuntimeType()).thenReturn(invocationRuntimeType);
+      Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
+      Mockito.when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
+      Mockito.when(microserviceMeta.getHandlerChain()).thenReturn(new ArrayList<>());
+
+      InvocationException throwable = (InvocationException) catchThrowable(() -> creator.createAsync().join());
+      CommonExceptionData data = (CommonExceptionData) throwable.getErrorData();
+
+      assertThat(throwable.getStatusCode()).isEqualTo(NOT_ACCEPTABLE.getStatusCode());
+      assertThat(Json.encode(data))
+              .isEqualTo("{\"code\":\"SCB.00000000\",\"message\":\"Accept test-type is not supported\"}");
+    }
   }
 
   @Test
   public void should_save_requestEx_in_invocation_context() {
-    mockGetServicePathManager();
-
-    Invocation invocation = creator.createAsync().join();
-
-    Object request = invocation.getLocalContext(RestConst.REST_REQUEST);
-    assertThat(request).isSameAs(requestEx);
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(microserviceMeta)).thenReturn(servicePathManager);
+      Mockito.when(creator.locateOperation(microserviceMeta)).thenReturn(locator);
+      Mockito.when(locator.getOperation()).thenReturn(restOperationMeta);
+      Mockito.when(restOperationMeta.getOperationMeta()).thenReturn(operationMeta);
+      Mockito.when(operationMeta.buildBaseProviderRuntimeType()).thenReturn(invocationRuntimeType);
+      Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
+      Mockito.when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
+      Mockito.when(microserviceMeta.getHandlerChain()).thenReturn(new ArrayList<>());
+      Mockito.doNothing().when(creator).initProduceProcessor();
+
+      Invocation invocation = creator.createAsync().join();
+
+      Object request = invocation.getLocalContext(RestConst.REST_REQUEST);
+      assertThat(request).isSameAs(requestEx);
+    }
   }
 
   @Test
   public void should_save_path_var_map_in_requestEx() {
-    mockGetServicePathManager();
-
-    creator.createAsync().join();
-
-    new Verifications() {
-      {
-        requestEx.setAttribute(RestConst.PATH_PARAMETERS, any);
-        times = 1;
-      }
-    };
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(microserviceMeta)).thenReturn(servicePathManager);
+      Mockito.when(creator.locateOperation(microserviceMeta)).thenReturn(locator);
+      Mockito.when(locator.getOperation()).thenReturn(restOperationMeta);
+      Mockito.when(restOperationMeta.getOperationMeta()).thenReturn(operationMeta);
+      Mockito.when(operationMeta.buildBaseProviderRuntimeType()).thenReturn(invocationRuntimeType);
+      Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
+      Mockito.when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
+      Mockito.when(microserviceMeta.getHandlerChain()).thenReturn(new ArrayList<>());
+      Mockito.doNothing().when(creator).initProduceProcessor();
+
+      creator.createAsync().join();
+
+      Mockito.verify(requestEx, Mockito.times(1)).setAttribute(Mockito.eq(RestConst.PATH_PARAMETERS), Mockito.any());
+    }
   }
 
   @Test
   public void should_merge_invocation_context_from_request() {
-    mockGetServicePathManager();
-    new Expectations() {
-      {
-        requestEx.getHeader(Const.CSE_CONTEXT);
-        result = "{\"k\":\"v\"}";
-      }
-    };
-
-    Invocation invocation = creator.createAsync().join();
-
-    assertThat(invocation.getContext("k")).isEqualTo("v");
+    try (MockedStatic<ServicePathManager> mockedStatic = Mockito.mockStatic(ServicePathManager.class)) {
+      mockedStatic.when(() -> ServicePathManager.getServicePathManager(microserviceMeta)).thenReturn(servicePathManager);
+      Mockito.when(creator.locateOperation(microserviceMeta)).thenReturn(locator);
+      Mockito.when(locator.getOperation()).thenReturn(restOperationMeta);
+      Mockito.when(restOperationMeta.getOperationMeta()).thenReturn(operationMeta);
+      Mockito.when(operationMeta.buildBaseProviderRuntimeType()).thenReturn(invocationRuntimeType);
+      Mockito.when(operationMeta.getSchemaMeta()).thenReturn(schemaMeta);
+      Mockito.when(schemaMeta.getMicroserviceMeta()).thenReturn(microserviceMeta);
+      Mockito.when(microserviceMeta.getHandlerChain()).thenReturn(new ArrayList<>());
+      Mockito.doNothing().when(creator).initProduceProcessor();
+      Mockito.when(requestEx.getHeader(Const.CSE_CONTEXT)).thenReturn("{\"k\":\"v\"}");
+
+      Invocation invocation = creator.createAsync().join();
+
+      assertThat(invocation.getContext("k")).isEqualTo("v");

Review Comment:
   can we use `Assert.assertEquals`?



##########
common/common-rest/src/test/java/org/apache/servicecomb/common/rest/TestRestProducerInvocation.java:
##########
@@ -119,14 +113,22 @@ void findRestOperation() {
       void scheduleInvocation() {
         throw new IllegalStateException("must not invoke scheduleInvocation");
       }
-    }.getMockInstance();
+    }.getMockInstance();*/
+    restProducerInvocation = Mockito.mock(RestProducerInvocation.class);
+    Mockito.doAnswer(invocationOnMock -> {
+      throwableOfSendFailResponse = expected;
+      return null;
+    }).when(restProducerInvocation).sendFailResponse(Mockito.any());
+    Mockito.doThrow(expected).when(restProducerInvocation).findRestOperation();
+    Mockito.doThrow(new IllegalStateException("must not invoke scheduleInvocation")).when(restProducerInvocation).scheduleInvocation();
+
 
     restProducerInvocation.invoke(transport, requestEx, responseEx, httpServerFilters);
 
     Assertions.assertSame(expected, throwableOfSendFailResponse);
   }
 
-  @Test
+  /*@Test

Review Comment:
   We should delete commented out codes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@servicecomb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org