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 25b5b4802 [SCB-2707]migrate provider-springmvc to mockito(#3423)
25b5b4802 is described below
commit 25b5b4802326368e9885fe10dea0c2997a6cb98c
Author: Tian Luo <[email protected]>
AuthorDate: Sat Oct 29 10:28:37 2022 +0800
[SCB-2707]migrate provider-springmvc to mockito(#3423)
---
providers/provider-springmvc/pom.xml | 4 +-
.../springmvc/reference/CseClientHttpRequest.java | 3 +-
.../servicecomb/provider/common/MockUtil.java | 61 ----------------------
.../reference/TestCseRequestCallback.java | 12 ++---
.../TestRestTemplateCopyHeaderFilter.java | 41 ++++-----------
...WithProviderPrefixClientHttpRequestFactory.java | 39 ++++++--------
...UrlWithServiceNameClientHttpRequestFactory.java | 40 ++++++--------
.../async/CseAsyncRequestCallbackTest.java | 13 ++---
8 files changed, 61 insertions(+), 152 deletions(-)
diff --git a/providers/provider-springmvc/pom.xml
b/providers/provider-springmvc/pom.xml
index 23021461e..f43d8f17f 100644
--- a/providers/provider-springmvc/pom.xml
+++ b/providers/provider-springmvc/pom.xml
@@ -67,8 +67,8 @@
<scope>test</scope>
</dependency>
<dependency>
- <groupId>org.jmockit</groupId>
- <artifactId>jmockit</artifactId>
+ <groupId>org.mockito</groupId>
+ <artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
diff --git
a/providers/provider-springmvc/src/main/java/org/apache/servicecomb/provider/springmvc/reference/CseClientHttpRequest.java
b/providers/provider-springmvc/src/main/java/org/apache/servicecomb/provider/springmvc/reference/CseClientHttpRequest.java
index 6bcf88b5d..97d0e9e8c 100644
---
a/providers/provider-springmvc/src/main/java/org/apache/servicecomb/provider/springmvc/reference/CseClientHttpRequest.java
+++
b/providers/provider-springmvc/src/main/java/org/apache/servicecomb/provider/springmvc/reference/CseClientHttpRequest.java
@@ -94,7 +94,8 @@ public class CseClientHttpRequest implements
ClientHttpRequest {
return requestMeta;
}
- protected void setRequestMeta(RequestMeta requestMeta) {
+ @VisibleForTesting
+ public void setRequestMeta(RequestMeta requestMeta) {
this.requestMeta = requestMeta;
}
diff --git
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/common/MockUtil.java
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/common/MockUtil.java
deleted file mode 100644
index c0eb8f609..000000000
---
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/common/MockUtil.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.servicecomb.provider.common;
-
-import java.lang.reflect.Field;
-
-import
org.apache.servicecomb.provider.springmvc.reference.CseClientHttpRequest;
-import
org.apache.servicecomb.provider.springmvc.reference.CseClientHttpResponse;
-import org.apache.servicecomb.swagger.invocation.Response;
-import org.springframework.util.ReflectionUtils;
-
-import mockit.Mock;
-import mockit.MockUp;
-
-public class MockUtil {
- private static final MockUtil instance = new MockUtil();
-
- private MockUtil() {
-
- }
-
- public static MockUtil getInstance() {
- return instance;
- }
-
- public void mockReflectionUtils() {
-
- new MockUp<ReflectionUtils>() {
- @Mock
- Object getField(Field field, Object target) {
- Response response = Response.ok(200);
- return new CseClientHttpResponse(response);
- }
- };
- }
-
- public void mockCseClientHttpRequest() {
-
- new MockUp<CseClientHttpRequest>() {
- @Mock
- public void setRequestBody(Object requestBody) {
-
- }
- };
- }
-}
diff --git
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestCseRequestCallback.java
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestCseRequestCallback.java
index 97c16990f..f1dc54ac5 100644
---
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestCseRequestCallback.java
+++
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestCseRequestCallback.java
@@ -20,14 +20,13 @@ import java.io.IOException;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
+import org.mockito.Mockito;
import org.springframework.web.client.RequestCallback;
-import mockit.Injectable;
-import mockit.Mocked;
-
public class TestCseRequestCallback {
@Test
- public void testNormal(@Mocked RequestCallback callback) throws IOException {
+ public void testNormal() throws IOException {
+ RequestCallback callback = Mockito.mock(RequestCallback.class);
CseClientHttpRequest request = new CseClientHttpRequest();
CseRequestCallback cb = new CseRequestCallback(null, callback, null);
cb.doWithRequest(request);
@@ -36,8 +35,9 @@ public class TestCseRequestCallback {
}
@Test
- public void testCseEntity(@Injectable CseHttpEntity<?> entity,
- @Mocked RequestCallback callback) throws IOException {
+ public void testCseEntity() throws IOException {
+ CseHttpEntity<?> entity = Mockito.mock(CseHttpEntity.class);
+ RequestCallback callback = Mockito.mock(RequestCallback.class);
CseClientHttpRequest request = new CseClientHttpRequest();
entity.addContext("c1", "c2");
diff --git
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestRestTemplateCopyHeaderFilter.java
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestRestTemplateCopyHeaderFilter.java
index 7e02c75ea..9ca007251 100644
---
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestRestTemplateCopyHeaderFilter.java
+++
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestRestTemplateCopyHeaderFilter.java
@@ -29,28 +29,22 @@ import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
+import org.mockito.Mockito;
import org.springframework.http.HttpHeaders;
-import mockit.Expectations;
-import mockit.Mocked;
-
public class TestRestTemplateCopyHeaderFilter {
RestTemplateCopyHeaderFilter filter = new RestTemplateCopyHeaderFilter();
+ Invocation invocation = Mockito.mock(Invocation.class);
@Test
public void getOrder() {
Assertions.assertEquals(-10000, filter.getOrder());
}
@Test
- public void beforeSendRequestNoHeader(@Mocked Invocation invocation) throws
ExecutionException, InterruptedException {
+ public void beforeSendRequestNoHeader() throws ExecutionException,
InterruptedException {
Map<String, Object> context = new HashMap<>();
- new Expectations() {
- {
- invocation.getHandlerContext();
- result = context;
- }
- };
+ Mockito.when(invocation.getHandlerContext()).thenReturn(context);
HttpServletRequestEx requestEx = new CommonToHttpServletRequest(null,
null, new HttpHeaders(), null, false);
filter.beforeSendRequestAsync(invocation, requestEx).get();
@@ -58,7 +52,7 @@ public class TestRestTemplateCopyHeaderFilter {
}
@Test
- public void beforeSendRequestWithNullHeader(@Mocked Invocation invocation)
+ public void beforeSendRequestWithNullHeader()
throws ExecutionException, InterruptedException {
Map<String, Object> context = new HashMap<>(1);
HttpHeaders httpHeaders = new HttpHeaders();
@@ -66,12 +60,7 @@ public class TestRestTemplateCopyHeaderFilter {
httpHeaders.add("headerName0", "headerValue0");
httpHeaders.add("headerName1", null);
httpHeaders.add("headerName2", "headerValue2");
- new Expectations() {
- {
- invocation.getHandlerContext();
- result = context;
- }
- };
+ Mockito.when(invocation.getHandlerContext()).thenReturn(context);
HttpServletRequestEx requestEx = new CommonToHttpServletRequest(null,
null, new HttpHeaders(), null, false);
filter.beforeSendRequestAsync(invocation, requestEx).get();
@@ -81,19 +70,14 @@ public class TestRestTemplateCopyHeaderFilter {
}
@Test
- public void beforeSendRequestHaveHeader(@Mocked Invocation invocation)
+ public void beforeSendRequestHaveHeader()
throws ExecutionException, InterruptedException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("name", "value");
Map<String, Object> context = new HashMap<>();
context.put(RestConst.CONSUMER_HEADER, httpHeaders);
- new Expectations() {
- {
- invocation.getHandlerContext();
- result = context;
- }
- };
+ Mockito.when(invocation.getHandlerContext()).thenReturn(context);
HttpServletRequestEx requestEx = new CommonToHttpServletRequest(null,
null, new HttpHeaders(), null, false);
filter.beforeSendRequestAsync(invocation, requestEx).get();
@@ -101,19 +85,14 @@ public class TestRestTemplateCopyHeaderFilter {
}
@Test
- public void beforeSendRequestSkipContentLength(@Mocked Invocation invocation)
+ public void beforeSendRequestSkipContentLength()
throws ExecutionException, InterruptedException {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add(HttpHeaders.CONTENT_LENGTH, "0");
Map<String, Object> context = new HashMap<>();
context.put(RestConst.CONSUMER_HEADER, httpHeaders);
- new Expectations() {
- {
- invocation.getHandlerContext();
- result = context;
- }
- };
+ Mockito.when(invocation.getHandlerContext()).thenReturn(context);
HttpServletRequestEx requestEx = new CommonToHttpServletRequest(null,
null, new HttpHeaders(), null, false);
filter.beforeSendRequestAsync(invocation, requestEx).get();
diff --git
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestUrlWithProviderPrefixClientHttpRequestFactory.java
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestUrlWithProviderPrefixClientHttpRequestFactory.java
index 85dcadfe7..b66f6990b 100644
---
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestUrlWithProviderPrefixClientHttpRequestFactory.java
+++
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestUrlWithProviderPrefixClientHttpRequestFactory.java
@@ -24,20 +24,16 @@ import java.util.Map;
import org.apache.servicecomb.common.rest.RestConst;
import org.apache.servicecomb.core.Invocation;
-import org.apache.servicecomb.core.definition.InvocationRuntimeType;
import org.apache.servicecomb.core.definition.OperationMeta;
import org.apache.servicecomb.core.invocation.InvocationFactory;
-import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
import
org.apache.servicecomb.provider.springmvc.reference.UrlWithProviderPrefixClientHttpRequestFactory.UrlWithProviderPrefixClientHttpRequest;
import org.apache.servicecomb.swagger.invocation.Response;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
import org.springframework.http.HttpMethod;
-import mockit.Deencapsulation;
-import mockit.Expectations;
-import mockit.Mocked;
-
public class TestUrlWithProviderPrefixClientHttpRequestFactory {
UrlWithProviderPrefixClientHttpRequestFactory factory = new
UrlWithProviderPrefixClientHttpRequestFactory("/a/b/c");
@@ -53,8 +49,12 @@ public class
TestUrlWithProviderPrefixClientHttpRequestFactory {
@Test
@SuppressWarnings("unchecked")
- public void invoke_checkPath(@Mocked Invocation invocation, @Mocked
RequestMeta requestMeta) {
- Map<String, String> handlerContext = new HashMap<>();
+ public void invoke_checkPath() {
+ Invocation invocation = Mockito.mock(Invocation.class);
+ RequestMeta requestMeta = Mockito.mock(RequestMeta.class);
+ OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
+ Mockito.when(requestMeta.getOperationMeta()).thenReturn(operationMeta);
+ Map<String, Object> handlerContext = new HashMap<>();
UrlWithProviderPrefixClientHttpRequest request = new
UrlWithProviderPrefixClientHttpRequest(uri, HttpMethod.GET,
"/a/b/c") {
@Override
@@ -63,21 +63,16 @@ public class
TestUrlWithProviderPrefixClientHttpRequestFactory {
}
};
- new Expectations(InvocationFactory.class) {
- {
- invocation.getHandlerContext();
- result = handlerContext;
- InvocationFactory.forConsumer((ReferenceConfig) any, (OperationMeta)
any, (InvocationRuntimeType) any,
- (Map<String, Object>) any);
- result = invocation;
- }
- };
-
- Deencapsulation.setField(request, "requestMeta", requestMeta);
- Deencapsulation.setField(request, "path", request.findUriPath(uri));
+ Mockito.when(invocation.getHandlerContext()).thenReturn(handlerContext);
+ try (MockedStatic<InvocationFactory> invocationFactoryMockedStatic =
Mockito.mockStatic(InvocationFactory.class)) {
+ invocationFactoryMockedStatic.when(() ->
InvocationFactory.forConsumer(Mockito.any(), Mockito.any(), Mockito.any(),
Mockito.any()))
+ .thenReturn(invocation);
+ request.setRequestMeta(requestMeta);
+ request.setPath(request.findUriPath(uri));
- request.invoke(new HashMap<>());
+ request.invoke(new HashMap<>());
- Assertions.assertEquals("/v1/path",
handlerContext.get(RestConst.REST_CLIENT_REQUEST_PATH));
+ Assertions.assertEquals("/v1/path",
handlerContext.get(RestConst.REST_CLIENT_REQUEST_PATH));
+ }
}
}
diff --git
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestUrlWithServiceNameClientHttpRequestFactory.java
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestUrlWithServiceNameClientHttpRequestFactory.java
index d81d38150..ef4362616 100644
---
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestUrlWithServiceNameClientHttpRequestFactory.java
+++
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/TestUrlWithServiceNameClientHttpRequestFactory.java
@@ -24,20 +24,16 @@ import java.util.Map;
import org.apache.servicecomb.common.rest.RestConst;
import org.apache.servicecomb.core.Invocation;
-import org.apache.servicecomb.core.definition.InvocationRuntimeType;
import org.apache.servicecomb.core.definition.OperationMeta;
import org.apache.servicecomb.core.invocation.InvocationFactory;
-import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
import
org.apache.servicecomb.provider.springmvc.reference.UrlWithServiceNameClientHttpRequestFactory.UrlWithServiceNameClientHttpRequest;
import org.apache.servicecomb.swagger.invocation.Response;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
import org.springframework.http.HttpMethod;
-import mockit.Deencapsulation;
-import mockit.Expectations;
-import mockit.Mocked;
-
public class TestUrlWithServiceNameClientHttpRequestFactory {
UrlWithServiceNameClientHttpRequestFactory factory = new
UrlWithServiceNameClientHttpRequestFactory();
@@ -53,8 +49,12 @@ public class TestUrlWithServiceNameClientHttpRequestFactory {
@Test
@SuppressWarnings("unchecked")
- public void invoke_checkPath(@Mocked Invocation invocation, @Mocked
RequestMeta requestMeta) {
- Map<String, String> handlerContext = new HashMap<>();
+ public void invoke_checkPath() {
+ Invocation invocation = Mockito.mock(Invocation.class);
+ RequestMeta requestMeta = Mockito.mock(RequestMeta.class);
+ OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
+ Mockito.when(requestMeta.getOperationMeta()).thenReturn(operationMeta);
+ Map<String, Object> handlerContext = new HashMap<>();
UrlWithServiceNameClientHttpRequest request = new
UrlWithServiceNameClientHttpRequest(uri, HttpMethod.GET) {
@Override
protected Response doInvoke(Invocation invocation) {
@@ -62,21 +62,15 @@ public class TestUrlWithServiceNameClientHttpRequestFactory
{
}
};
- new Expectations(InvocationFactory.class) {
- {
- invocation.getHandlerContext();
- result = handlerContext;
- InvocationFactory.forConsumer((ReferenceConfig) any, (OperationMeta)
any, (InvocationRuntimeType) any,
- (Map<String, Object>) any);
- result = invocation;
- }
- };
-
- Deencapsulation.setField(request, "requestMeta", requestMeta);
- Deencapsulation.setField(request, "path", request.findUriPath(uri));
-
- request.invoke(new HashMap<>());
+ Mockito.when(invocation.getHandlerContext()).thenReturn(handlerContext);
+ try (MockedStatic<InvocationFactory> invocationFactoryMockedStatic =
Mockito.mockStatic(InvocationFactory.class)) {
+ invocationFactoryMockedStatic.when(() ->
InvocationFactory.forConsumer(Mockito.any(), Mockito.any(), Mockito.any(),
Mockito.any()))
+ .thenReturn(invocation);
+ request.setRequestMeta(requestMeta);
+ request.setPath(request.findUriPath(uri));
+ request.invoke(new HashMap<>());
- Assertions.assertEquals("/ms/v1/path",
handlerContext.get(RestConst.REST_CLIENT_REQUEST_PATH));
+ Assertions.assertEquals("/ms/v1/path",
handlerContext.get(RestConst.REST_CLIENT_REQUEST_PATH));
+ }
}
}
diff --git
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/async/CseAsyncRequestCallbackTest.java
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/async/CseAsyncRequestCallbackTest.java
index 80280dac6..da3d7521d 100644
---
a/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/async/CseAsyncRequestCallbackTest.java
+++
b/providers/provider-springmvc/src/test/java/org/apache/servicecomb/provider/springmvc/reference/async/CseAsyncRequestCallbackTest.java
@@ -20,10 +20,9 @@ package
org.apache.servicecomb.provider.springmvc.reference.async;
import org.apache.servicecomb.provider.springmvc.reference.CseHttpEntity;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
+import org.mockito.Mockito;
import org.springframework.http.HttpEntity;
-import mockit.Injectable;
-
public class CseAsyncRequestCallbackTest {
@Test
public void testNormal() {
@@ -34,18 +33,20 @@ public class CseAsyncRequestCallbackTest {
}
@Test
- public void testHttpEntity(@Injectable HttpEntity<String> entity) {
- CseAsyncRequestCallback<String> cb = new CseAsyncRequestCallback<>(entity);
+ public void testHttpEntity() {
+ HttpEntity<?> entity = Mockito.mock(HttpEntity.class);
+ CseAsyncRequestCallback<?> cb = new CseAsyncRequestCallback<>(entity);
CseAsyncClientHttpRequest request = new CseAsyncClientHttpRequest();
cb.doWithRequest(request);
Assertions.assertEquals(entity.getBody(), request.getBody());
}
@Test
- public void testCseEntity(@Injectable CseHttpEntity<String> entity) {
+ public void testCseEntity() {
+ CseHttpEntity<?> entity = Mockito.mock(CseHttpEntity.class);
CseAsyncClientHttpRequest request = new CseAsyncClientHttpRequest();
entity.addContext("c1", "c2");
- CseAsyncRequestCallback<String> cb = new CseAsyncRequestCallback<>(entity);
+ CseAsyncRequestCallback<?> cb = new CseAsyncRequestCallback<>(entity);
cb.doWithRequest(request);
Assertions.assertEquals(entity.getContext(), request.getContext());
}