blob: 224060c9d912e20ee85d4be2ff3c3906be7f1648 [file] [log] [blame]
/**
* 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.pulsar.broker;
import java.util.UUID;
import org.mockito.Mockito;
/**
* Holds util methods used in test.
*/
public class BrokerTestUtil {
// Generate unique name for different test run.
public static String newUniqueName(String prefix) {
return prefix + "-" + UUID.randomUUID();
}
/**
* Creates a Mockito spy directly without an intermediate instance to spy.
* This is to address flaky test issue where a spy created with a given instance fails with
* {@link org.mockito.exceptions.misusing.WrongTypeOfReturnValue} exception.
*
* @param classToSpy the class to spy
* @param args the constructor arguments to use when creating the spy instance
* @return a spy of the provided class created with given constructor arguments
*/
public static <T> T spyWithClassAndConstructorArgs(Class<T> classToSpy, Object... args) {
return Mockito.mock(classToSpy, Mockito.withSettings()
.useConstructor(args)
.defaultAnswer(Mockito.CALLS_REAL_METHODS));
}
}