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 31da26516 migrate provider-rest-common to mockito (#3422)
31da26516 is described below
commit 31da265166389db185478679582d2edb4dbfd463
Author: Tian Luo <[email protected]>
AuthorDate: Sun Oct 23 19:08:37 2022 +0800
migrate provider-rest-common to mockito (#3422)
---
providers/provider-rest-common/pom.xml | 4 +-
.../common/TestInvocationToHttpServletRequest.java | 200 ++++++---------------
.../common/TestProducerHttpRequestArgMapper.java | 35 ++--
.../rest/common/TestRestServiceProvider.java | 27 ++-
4 files changed, 82 insertions(+), 184 deletions(-)
diff --git a/providers/provider-rest-common/pom.xml
b/providers/provider-rest-common/pom.xml
index 09171b944..fba62ed60 100644
--- a/providers/provider-rest-common/pom.xml
+++ b/providers/provider-rest-common/pom.xml
@@ -50,8 +50,8 @@
<artifactId>foundation-test-scaffolding</artifactId>
</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-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestInvocationToHttpServletRequest.java
b/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestInvocationToHttpServletRequest.java
index b6b395315..6e20e7ac1 100644
---
a/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestInvocationToHttpServletRequest.java
+++
b/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestInvocationToHttpServletRequest.java
@@ -38,25 +38,19 @@ import org.junit.Before;
import org.junit.Test;
import io.vertx.core.net.SocketAddress;
-import mockit.Expectations;
-import mockit.Mocked;
import org.junit.jupiter.api.Assertions;
import org.mockito.Mockito;
public class TestInvocationToHttpServletRequest {
- @Mocked
- Invocation invocation;
+ Invocation invocation = Mockito.mock(Invocation.class);
- @Mocked
- OperationMeta operationMeta;
+ OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
- @Mocked
- RestOperationMeta swaggerOperation;
+ RestOperationMeta swaggerOperation = Mockito.mock(RestOperationMeta.class);
Map<String, Object> args;
- @Mocked
- SocketAddress socketAddress;
+ SocketAddress socketAddress = Mockito.mock(SocketAddress.class);
Map<String, Object> handlerContext = new HashMap<>();
@@ -67,14 +61,8 @@ public class TestInvocationToHttpServletRequest {
handlerContext.put(Const.REMOTE_ADDRESS, socketAddress);
args = new HashMap<>();
- new Expectations() {
- {
- invocation.getOperationMeta();
- result = operationMeta;
- operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
- result = swaggerOperation;
- }
- };
+ Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
+
Mockito.when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerOperation);
request = new InvocationToHttpServletRequest(invocation);
}
@@ -86,86 +74,54 @@ public class TestInvocationToHttpServletRequest {
@Test
public void testGetParameterNotFound() {
- new Expectations() {
- {
- swaggerOperation.getParamByName("name");
- result = null;
- }
- };
+ Mockito.when(swaggerOperation.getParamByName("name")).thenReturn(null);
Assertions.assertNull(request.getParameter("name"));
}
@Test
- public void testGetParameterNull(@Mocked RestParam restParam) {
- new Expectations() {
- {
- swaggerOperation.getParamByName("name");
- result = restParam;
- restParam.getValue(args);
- result = null;
- }
- };
+ public void testGetParameterNull() {
+ RestParam restParam = Mockito.mock(RestParam.class);
+
Mockito.when(swaggerOperation.getParamByName("name")).thenReturn(restParam);
+ Mockito.when(restParam.getValue(args)).thenReturn(null);
Assertions.assertNull(request.getParameter("name"));
}
@Test
- public void testGetParameterNormal(@Mocked RestParam restParam) {
- new Expectations() {
- {
- swaggerOperation.getParamByName("name");
- result = restParam;
- restParam.getValue(args);
- result = "value";
- }
- };
+ public void testGetParameterNormal() {
+ RestParam restParam = Mockito.mock(RestParam.class);
+
Mockito.when(swaggerOperation.getParamByName("name")).thenReturn(restParam);
+ Mockito.when(restParam.getValue(args)).thenReturn("value");
Assertions.assertEquals("value", request.getParameter("name"));
}
@Test
public void testGetParameterValuesNotFound() {
- new Expectations() {
- {
- swaggerOperation.getParamByName("name");
- result = null;
- }
- };
+ Mockito.when(swaggerOperation.getParamByName("name")).thenReturn(null);
Assertions.assertNull(request.getParameterValues("name"));
}
@Test
- public void testGetParameterValuesNormal(@Mocked RestParam restParam) {
- new Expectations() {
- {
- swaggerOperation.getParamByName("name");
- result = restParam;
- restParam.getValueAsStrings(args);
- result = new String[] {"value"};
- }
- };
+ public void testGetParameterValuesNormal() {
+ RestParam restParam = Mockito.mock(RestParam.class);
+
Mockito.when(swaggerOperation.getParamByName("name")).thenReturn(restParam);
+ Mockito.when(restParam.getValueAsStrings(args)).thenReturn(new String[]
{"value"});
MatcherAssert.assertThat(request.getParameterValues("name"),
Matchers.arrayContaining("value"));
}
@Test
- public void testGetParameterMap(@Mocked RestParam p1, @Mocked RestParam p2) {
- new Expectations() {
- {
- swaggerOperation.getParamList();
- result = Arrays.asList(p1, p2);
- p1.getValueAsStrings(args);
- result = new String[] {"v1"};
- p1.getParamName();
- result = "p1";
- p2.getValueAsStrings(args);
- result = new String[] {"v2"};
- p2.getParamName();
- result = "p2";
- }
- };
+ public void testGetParameterMap() {
+ RestParam p1 = Mockito.mock(RestParam.class);
+ RestParam p2 = Mockito.mock(RestParam.class);
+ Mockito.when(swaggerOperation.getParamList()).thenReturn(Arrays.asList(p1,
p2));
+ Mockito.when(p1.getValueAsStrings(args)).thenReturn(new String[] {"v1"});
+ Mockito.when(p1.getParamName()).thenReturn("p1");
+ Mockito.when(p2.getValueAsStrings(args)).thenReturn(new String[] {"v2"});
+ Mockito.when(p2.getParamName()).thenReturn("p2");
Map<String, String[]> params = request.getParameterMap();
MatcherAssert.assertThat(params.size(), Matchers.is(2));
@@ -174,43 +130,28 @@ public class TestInvocationToHttpServletRequest {
}
@Test
- public void testGetHeader(@Mocked RestParam restParam) {
- new Expectations() {
- {
- swaggerOperation.getParamByName("name");
- result = restParam;
- restParam.getValue(args);
- result = "value";
- }
- };
+ public void testGetHeader() {
+ RestParam restParam = Mockito.mock(RestParam.class);
+
Mockito.when(swaggerOperation.getParamByName("name")).thenReturn(restParam);
+ Mockito.when(restParam.getValue(args)).thenReturn("value");
Assertions.assertEquals("value", request.getHeader("name"));
}
@Test
- public void testGetIntHeaderNotFound(@Mocked RestParam restParam) {
- new Expectations() {
- {
- swaggerOperation.getParamByName("name");
- result = restParam;
- restParam.getValue(args);
- result = null;
- }
- };
+ public void testGetIntHeaderNotFound() {
+ RestParam restParam = Mockito.mock(RestParam.class);
+
Mockito.when(swaggerOperation.getParamByName("name")).thenReturn(restParam);
+ Mockito.when(restParam.getValue(args)).thenReturn(null);
Assertions.assertEquals(-1, request.getIntHeader("name"));
}
@Test
- public void testGetIntHeaderNotNumber(@Mocked RestParam restParam) {
- new Expectations() {
- {
- swaggerOperation.getParamByName("name");
- result = restParam;
- restParam.getValue(args);
- result = "value";
- }
- };
+ public void testGetIntHeaderNotNumber() {
+ RestParam restParam = Mockito.mock(RestParam.class);
+
Mockito.when(swaggerOperation.getParamByName("name")).thenReturn(restParam);
+ Mockito.when(restParam.getValue(args)).thenReturn("value");
try {
request.getIntHeader("name");
@@ -221,55 +162,35 @@ public class TestInvocationToHttpServletRequest {
}
@Test
- public void testGetIntHeaderNormal(@Mocked RestParam restParam) {
- new Expectations() {
- {
- swaggerOperation.getParamByName("name");
- result = restParam;
- restParam.getValue(args);
- result = "1";
- }
- };
+ public void testGetIntHeaderNormal() {
+ RestParam restParam = Mockito.mock(RestParam.class);
+
Mockito.when(swaggerOperation.getParamByName("name")).thenReturn(restParam);
+ Mockito.when(restParam.getValue(args)).thenReturn("1");
Assertions.assertEquals(1, request.getIntHeader("name"));
}
@Test
public void testGetMethod() {
- new Expectations() {
- {
- swaggerOperation.getHttpMethod();
- result = "GET";
- }
- };
+ Mockito.when(swaggerOperation.getHttpMethod()).thenReturn("GET");
Assertions.assertEquals("GET", request.getMethod());
}
@Test
- public void testGetPathInfoNormal(@Mocked URLPathBuilder builder) throws
Exception {
- new Expectations() {
- {
- swaggerOperation.getPathBuilder();
- result = builder;
- builder.createPathString(args);
- result = "/path";
- }
- };
+ public void testGetPathInfoNormal() throws Exception {
+ URLPathBuilder builder = Mockito.mock(URLPathBuilder.class);
+ Mockito.when(swaggerOperation.getPathBuilder()).thenReturn(builder);
+ Mockito.when(builder.createPathString(args)).thenReturn("/path");
Assertions.assertEquals("/path", request.getPathInfo());
}
@Test
- public void testGetPathInfoException(@Mocked URLPathBuilder builder) throws
Exception {
- new Expectations() {
- {
- swaggerOperation.getPathBuilder();
- result = builder;
- builder.createPathString(args);
- result = new Exception("error");
- }
- };
+ public void testGetPathInfoException() throws Exception {
+ URLPathBuilder builder = Mockito.mock(URLPathBuilder.class);
+ Mockito.when(swaggerOperation.getPathBuilder()).thenReturn(builder);
+ Mockito.when(builder.createPathString(args)).thenThrow(new
Exception("error"));
try {
request.getPathInfo();
@@ -282,16 +203,9 @@ public class TestInvocationToHttpServletRequest {
@Test
public void testGetRemoteAddress() throws Exception {
- new Expectations() {
- {
- socketAddress.host();
- result = "127.0.0.2";
- socketAddress.port();
- result = 8088;
- invocation.getHandlerContext();
- result = handlerContext;
- }
- };
+ Mockito.when(socketAddress.host()).thenReturn("127.0.0.2");
+ Mockito.when(socketAddress.port()).thenReturn(8088);
+ Mockito.when(invocation.getHandlerContext()).thenReturn(handlerContext);
String addr = request.getRemoteAddr();
String host = request.getRemoteHost();
int port = request.getRemotePort();
@@ -319,7 +233,7 @@ public class TestInvocationToHttpServletRequest {
}
@Test
- public void testGetContextPath(@Mocked Invocation invocation) throws
Exception {
+ public void testGetContextPath() {
InvocationToHttpServletRequest request = new
InvocationToHttpServletRequest(invocation);
Assertions.assertEquals("", request.getContextPath());
}
diff --git
a/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestProducerHttpRequestArgMapper.java
b/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestProducerHttpRequestArgMapper.java
index e16ef6c53..da41b423b 100644
---
a/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestProducerHttpRequestArgMapper.java
+++
b/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestProducerHttpRequestArgMapper.java
@@ -28,46 +28,35 @@ import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.core.definition.OperationMeta;
import org.junit.Test;
-import mockit.Expectations;
-import mockit.Mocked;
import org.junit.jupiter.api.Assertions;
+import org.mockito.Mockito;
public class TestProducerHttpRequestArgMapper {
- @Mocked
- Invocation invocation;
+ Invocation invocation = Mockito.mock(Invocation.class);
ProducerHttpRequestArgMapper mapper = new
ProducerHttpRequestArgMapper("test", "test");
@Test
- public void testGetFromContext(@Mocked HttpServletRequest request) {
+ public void testGetFromContext() {
+ HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Map<String, Object> context = new HashMap<>();
context.put(RestConst.REST_REQUEST, request);
- new Expectations() {
- {
- invocation.getHandlerContext();
- result = context;
- }
- };
+ Mockito.when(invocation.getHandlerContext()).thenReturn(context);
Assertions.assertSame(request, mapper.createContextArg(invocation));
}
@Test
- public void testCreateFromInvocation(@Mocked HttpServletRequest request,
@Mocked OperationMeta operationMeta,
- @Mocked RestOperationMeta swaggerOperation) {
+ public void testCreateFromInvocation() {
+ HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+ OperationMeta operationMeta = Mockito.mock(OperationMeta.class);
+ RestOperationMeta swaggerOperation = Mockito.mock(RestOperationMeta.class);
Map<String, Object> context = new HashMap<>();
- new Expectations() {
- {
- invocation.getHandlerContext();
- result = context;
- invocation.getOperationMeta();
- result = operationMeta;
- operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION);
- result = swaggerOperation;
- }
- };
+ Mockito.when(invocation.getHandlerContext()).thenReturn(context);
+ Mockito.when(invocation.getOperationMeta()).thenReturn(operationMeta);
+
Mockito.when(operationMeta.getExtData(RestConst.SWAGGER_REST_OPERATION)).thenReturn(swaggerOperation);
Assertions.assertEquals(InvocationToHttpServletRequest.class,
mapper.createContextArg(invocation).getClass());
}
diff --git
a/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestRestServiceProvider.java
b/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestRestServiceProvider.java
index 6c5a77224..747c6fa70 100644
---
a/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestRestServiceProvider.java
+++
b/providers/provider-rest-common/src/test/java/org/apache/servicecomb/provider/rest/common/TestRestServiceProvider.java
@@ -21,25 +21,20 @@ import org.apache.servicecomb.common.rest.RestConst;
import org.apache.servicecomb.foundation.common.utils.BeanUtils;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
+import org.mockito.MockedStatic;
+import org.mockito.Mockito;
import org.springframework.context.ApplicationContext;
-import mockit.Expectations;
-import mockit.Mocked;
-
public class TestRestServiceProvider {
@Test
- public void testInit(@Mocked ApplicationContext context) {
- new Expectations(BeanUtils.class) {
- {
- BeanUtils.getContext();
- result = context;
- context.getBean(RestProducers.class);
- result = new RestProducers();
- }
- };
-
- RestProducerProvider restProducerProvider = new RestProducerProvider();
- restProducerProvider.init();
- Assertions.assertEquals(RestConst.REST, restProducerProvider.getName());
+ public void testInit() {
+ ApplicationContext context = Mockito.mock(ApplicationContext.class);
+ Mockito.when(context.getBean(RestProducers.class)).thenReturn(new
RestProducers());
+ try (MockedStatic<BeanUtils> beanUtilsMockedStatic =
Mockito.mockStatic(BeanUtils.class)) {
+ beanUtilsMockedStatic.when(BeanUtils::getContext).thenReturn(context);
+ RestProducerProvider restProducerProvider = new RestProducerProvider();
+ restProducerProvider.init();
+ Assertions.assertEquals(RestConst.REST, restProducerProvider.getName());
+ }
}
}