[hotfix][tests] Reduce mockito usage in tests
Project: http://git-wip-us.apache.org/repos/asf/flink/repo Commit: http://git-wip-us.apache.org/repos/asf/flink/commit/9ef10e65 Tree: http://git-wip-us.apache.org/repos/asf/flink/tree/9ef10e65 Diff: http://git-wip-us.apache.org/repos/asf/flink/diff/9ef10e65 Branch: refs/heads/release-1.5 Commit: 9ef10e659979d251d0e0bf1783e9322171f90f01 Parents: e1f64d8 Author: Piotr Nowojski <[email protected]> Authored: Wed May 9 11:29:18 2018 +0200 Committer: Tzu-Li (Gordon) Tai <[email protected]> Committed: Tue May 22 16:54:29 2018 +0800 ---------------------------------------------------------------------- .../operators/StreamingRuntimeContextTest.java | 10 +++--- .../operators/async/AsyncWaitOperatorTest.java | 9 +++--- .../flink/streaming/util/MockStreamConfig.java | 33 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/flink/blob/9ef10e65/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/StreamingRuntimeContextTest.java ---------------------------------------------------------------------- diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/StreamingRuntimeContextTest.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/StreamingRuntimeContextTest.java index 1072eec..87667b2 100644 --- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/StreamingRuntimeContextTest.java +++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/StreamingRuntimeContextTest.java @@ -42,6 +42,7 @@ import org.apache.flink.core.fs.Path; import org.apache.flink.runtime.execution.Environment; import org.apache.flink.runtime.jobgraph.JobVertexID; import org.apache.flink.runtime.operators.testutils.DummyEnvironment; +import org.apache.flink.runtime.operators.testutils.MockEnvironment; import org.apache.flink.runtime.query.KvStateRegistry; import org.apache.flink.runtime.state.AbstractKeyedStateBackend; import org.apache.flink.runtime.state.DefaultKeyedStateStore; @@ -58,7 +59,6 @@ import org.mockito.stubbing.Answer; import java.util.Collections; import java.util.Map; -import java.util.concurrent.Future; import java.util.concurrent.atomic.AtomicReference; import static org.junit.Assert.assertFalse; @@ -373,10 +373,8 @@ public class StreamingRuntimeContextTest { } private static Environment createMockEnvironment() { - Environment env = mock(Environment.class); - when(env.getUserClassLoader()).thenReturn(StreamingRuntimeContextTest.class.getClassLoader()); - when(env.getDistributedCacheEntries()).thenReturn(Collections.<String, Future<Path>>emptyMap()); - when(env.getTaskInfo()).thenReturn(new TaskInfo("test task", 1, 0, 1, 1)); - return env; + return MockEnvironment.builder() + .setTaskName("test task") + .build(); } } http://git-wip-us.apache.org/repos/asf/flink/blob/9ef10e65/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/async/AsyncWaitOperatorTest.java ---------------------------------------------------------------------- diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/async/AsyncWaitOperatorTest.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/async/AsyncWaitOperatorTest.java index cdd77d3..17d654e 100644 --- a/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/async/AsyncWaitOperatorTest.java +++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/api/operators/async/AsyncWaitOperatorTest.java @@ -58,6 +58,7 @@ import org.apache.flink.streaming.runtime.tasks.ProcessingTimeCallback; import org.apache.flink.streaming.runtime.tasks.ProcessingTimeService; import org.apache.flink.streaming.runtime.tasks.StreamTask; import org.apache.flink.streaming.runtime.tasks.TestProcessingTimeService; +import org.apache.flink.streaming.util.MockStreamConfig; import org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness; import org.apache.flink.streaming.util.TestHarnessUtil; import org.apache.flink.util.ExceptionUtils; @@ -680,8 +681,8 @@ public class AsyncWaitOperatorTest extends TestLogger { when(containingTask.getCheckpointLock()).thenReturn(lock); when(containingTask.getProcessingTimeService()).thenReturn(new TestProcessingTimeService()); - StreamConfig streamConfig = mock(StreamConfig.class); - doReturn(IntSerializer.INSTANCE).when(streamConfig).getTypeSerializerIn1(any(ClassLoader.class)); + StreamConfig streamConfig = new MockStreamConfig(); + streamConfig.setTypeSerializerIn1(IntSerializer.INSTANCE); final OneShotLatch closingLatch = new OneShotLatch(); final OneShotLatch outputLatch = new OneShotLatch(); @@ -783,8 +784,8 @@ public class AsyncWaitOperatorTest extends TestLogger { when(containingTask.getCheckpointLock()).thenReturn(lock); when(containingTask.getProcessingTimeService()).thenReturn(processingTimeService); - StreamConfig streamConfig = mock(StreamConfig.class); - doReturn(IntSerializer.INSTANCE).when(streamConfig).getTypeSerializerIn1(any(ClassLoader.class)); + StreamConfig streamConfig = new MockStreamConfig(); + streamConfig.setTypeSerializerIn1(IntSerializer.INSTANCE); Output<StreamRecord<Integer>> output = mock(Output.class); http://git-wip-us.apache.org/repos/asf/flink/blob/9ef10e65/flink-streaming-java/src/test/java/org/apache/flink/streaming/util/MockStreamConfig.java ---------------------------------------------------------------------- diff --git a/flink-streaming-java/src/test/java/org/apache/flink/streaming/util/MockStreamConfig.java b/flink-streaming-java/src/test/java/org/apache/flink/streaming/util/MockStreamConfig.java new file mode 100644 index 0000000..598da48 --- /dev/null +++ b/flink-streaming-java/src/test/java/org/apache/flink/streaming/util/MockStreamConfig.java @@ -0,0 +1,33 @@ +/* + * 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.flink.streaming.util; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.runtime.jobgraph.OperatorID; +import org.apache.flink.streaming.api.graph.StreamConfig; + +/** + * Handy mock for {@link StreamConfig}. + */ +public class MockStreamConfig extends StreamConfig { + public MockStreamConfig() { + super(new Configuration()); + + setOperatorID(new OperatorID()); + } +}
