You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by GitBox <gi...@apache.org> on 2022/05/28 02:20:14 UTC

[GitHub] [incubator-eventmesh] ruanwenjun commented on a diff in pull request #894: [ISSUE #859] Add unit tests for EventMesh SDK for Java

ruanwenjun commented on code in PR #894:
URL: https://github.com/apache/incubator-eventmesh/pull/894#discussion_r884053754


##########
eventmesh-sdk-java/src/test/java/org/apache/eventmesh/client/grpc/consumer/EventMeshGrpcConsumerTest.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.eventmesh.client.grpc.consumer;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+import static org.mockito.Mockito.when;
+
+import org.apache.eventmesh.client.grpc.config.EventMeshGrpcClientConfig;
+import org.apache.eventmesh.common.EventMeshMessage;
+import org.apache.eventmesh.common.protocol.SubscriptionItem;
+import org.apache.eventmesh.common.protocol.SubscriptionMode;
+import org.apache.eventmesh.common.protocol.SubscriptionType;
+import org.apache.eventmesh.common.protocol.grpc.protos.ConsumerServiceGrpc.ConsumerServiceBlockingStub;
+import org.apache.eventmesh.common.protocol.grpc.protos.ConsumerServiceGrpc.ConsumerServiceStub;
+import org.apache.eventmesh.common.protocol.grpc.protos.HeartbeatServiceGrpc.HeartbeatServiceBlockingStub;
+import org.apache.eventmesh.common.protocol.grpc.protos.Response;
+import org.apache.eventmesh.common.protocol.grpc.protos.SimpleMessage;
+import org.apache.eventmesh.common.protocol.grpc.protos.Subscription;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Optional;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.internal.util.reflection.FieldSetter;
+import org.powermock.core.classloader.annotations.PowerMockIgnore;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import io.grpc.stub.StreamObserver;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({ConsumerServiceBlockingStub.class, ConsumerServiceStub.class, HeartbeatServiceBlockingStub.class})
+@PowerMockIgnore({"javax.management.*", "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
+public class EventMeshGrpcConsumerTest {
+
+    @Mock
+    private ConsumerServiceBlockingStub consumerClient;
+    @Mock
+    private ConsumerServiceStub consumerAsyncClient;
+    @Mock
+    private HeartbeatServiceBlockingStub heartbeatClient;
+    private EventMeshGrpcConsumer eventMeshGrpcConsumer;
+
+    @Before
+    public void setUp() throws Exception {
+        eventMeshGrpcConsumer = new EventMeshGrpcConsumer(EventMeshGrpcClientConfig.builder().build());
+        eventMeshGrpcConsumer.init();
+        FieldSetter.setField(eventMeshGrpcConsumer, eventMeshGrpcConsumer.getClass().getDeclaredField("consumerClient"),
+            consumerClient);
+        FieldSetter.setField(eventMeshGrpcConsumer,
+            eventMeshGrpcConsumer.getClass().getDeclaredField("consumerAsyncClient"), consumerAsyncClient);
+        FieldSetter.setField(eventMeshGrpcConsumer,
+            eventMeshGrpcConsumer.getClass().getDeclaredField("heartbeatClient"), heartbeatClient);
+        when(consumerClient.subscribe(any())).thenReturn(Response.getDefaultInstance());

Review Comment:
   Use reflect to create test object is not a good practice, When someone changes the field name, he cannot find out the problem in compile.
   You can try to change the scope of field in `consumerClient`. The same to other test file.



-- 
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: dev-unsubscribe@eventmesh.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org