How to use JavaEightUtil method of org.mockito.internal.util.JavaEightUtil class

Best Mockito code snippet using org.mockito.internal.util.JavaEightUtil.JavaEightUtil

Source:ReturnsEmptyValues.java Github

copy

Full Screen

...6import static org.mockito.internal.util.ObjectMethodsGuru.isCompareToMethod;7import static org.mockito.internal.util.ObjectMethodsGuru.isToStringMethod;8import java.io.Serializable;9import java.util.*;10import org.mockito.internal.util.JavaEightUtil;11import org.mockito.internal.util.MockUtil;12import org.mockito.internal.util.Primitives;13import org.mockito.invocation.InvocationOnMock;14import org.mockito.mock.MockName;15import org.mockito.stubbing.Answer;16/**17 * Default answer of every Mockito mock.18 * <ul>19 * <li>20 * Returns appropriate primitive for primitive-returning methods21 * </li>22 * <li>23 * Returns consistent values for primitive wrapper classes (e.g. int-returning method returns 0 <b>and</b> Integer-returning method returns 0, too)24 * </li>25 * <li>26 * Returns empty collection for collection-returning methods (works for most commonly used collection types)27 * </li>28 * <li>29 * Returns description of mock for toString() method30 * </li>31 * <li>32 * Returns zero if references are equals otherwise non-zero for Comparable#compareTo(T other) method (see issue 184)33 * </li>34 * <li>35 * Returns an {@code java.util.Optional#empty() empty Optional} for Optional. Similarly for primitive optional variants.36 * </li>37 * <li>38 * Returns an {@code java.util.stream.Stream#empty() empty Stream} for Stream. Similarly for primitive stream variants.39 * </li>40 * <li>41 * Returns an {@code java.time.Duration.ZERO zero Duration} for empty Duration and {@code java.time.Period.ZERO zero Period} for empty Period.42 * </li>43 * <li>44 * Returns null for everything else45 * </li>46 * </ul>47 */48public class ReturnsEmptyValues implements Answer<Object>, Serializable {49 private static final long serialVersionUID = 1998191268711234347L;50 /* (non-Javadoc)51 * @see org.mockito.stubbing.Answer#answer(org.mockito.invocation.InvocationOnMock)52 */53 public Object answer(InvocationOnMock invocation) {54 if (isToStringMethod(invocation.getMethod())) {55 Object mock = invocation.getMock();56 MockName name = MockUtil.getMockName(mock);57 if (name.isDefault()) {58 return "Mock for "59 + MockUtil.getMockSettings(mock).getTypeToMock().getSimpleName()60 + ", hashCode: "61 + mock.hashCode();62 } else {63 return name.toString();64 }65 } else if (isCompareToMethod(invocation.getMethod())) {66 // see issue 184.67 // mocks by default should return 0 if references are the same, otherwise some other68 // value because they are not the same. Hence we return 1 (anything but 0 is good).69 // Only for compareTo() method by the Comparable interface70 return invocation.getMock() == invocation.getArgument(0) ? 0 : 1;71 }72 Class<?> returnType = invocation.getMethod().getReturnType();73 return returnValueFor(returnType);74 }75 Object returnValueFor(Class<?> type) {76 if (Primitives.isPrimitiveOrWrapper(type)) {77 return Primitives.defaultValue(type);78 // new instances are used instead of Collections.emptyList(), etc.79 // to avoid UnsupportedOperationException if code under test modifies returned80 // collection81 } else if (type == Iterable.class) {82 return new ArrayList<Object>(0);83 } else if (type == Collection.class) {84 return new LinkedList<Object>();85 } else if (type == Set.class) {86 return new HashSet<Object>();87 } else if (type == HashSet.class) {88 return new HashSet<Object>();89 } else if (type == SortedSet.class) {90 return new TreeSet<Object>();91 } else if (type == TreeSet.class) {92 return new TreeSet<Object>();93 } else if (type == LinkedHashSet.class) {94 return new LinkedHashSet<Object>();95 } else if (type == List.class) {96 return new LinkedList<Object>();97 } else if (type == LinkedList.class) {98 return new LinkedList<Object>();99 } else if (type == ArrayList.class) {100 return new ArrayList<Object>();101 } else if (type == Map.class) {102 return new HashMap<Object, Object>();103 } else if (type == HashMap.class) {104 return new HashMap<Object, Object>();105 } else if (type == SortedMap.class) {106 return new TreeMap<Object, Object>();107 } else if (type == TreeMap.class) {108 return new TreeMap<Object, Object>();109 } else if (type == LinkedHashMap.class) {110 return new LinkedHashMap<Object, Object>();111 } else if ("java.util.Optional".equals(type.getName())) {112 return JavaEightUtil.emptyOptional();113 } else if ("java.util.OptionalDouble".equals(type.getName())) {114 return JavaEightUtil.emptyOptionalDouble();115 } else if ("java.util.OptionalInt".equals(type.getName())) {116 return JavaEightUtil.emptyOptionalInt();117 } else if ("java.util.OptionalLong".equals(type.getName())) {118 return JavaEightUtil.emptyOptionalLong();119 } else if ("java.util.stream.Stream".equals(type.getName())) {120 return JavaEightUtil.emptyStream();121 } else if ("java.util.stream.DoubleStream".equals(type.getName())) {122 return JavaEightUtil.emptyDoubleStream();123 } else if ("java.util.stream.IntStream".equals(type.getName())) {124 return JavaEightUtil.emptyIntStream();125 } else if ("java.util.stream.LongStream".equals(type.getName())) {126 return JavaEightUtil.emptyLongStream();127 } else if ("java.time.Duration".equals(type.getName())) {128 return JavaEightUtil.emptyDuration();129 } else if ("java.time.Period".equals(type.getName())) {130 return JavaEightUtil.emptyPeriod();131 }132 // Let's not care about the rest of collections.133 return null;134 }135}...

Full Screen

Full Screen

Source:MutinyAnswer.java Github

copy

Full Screen

...18import java.util.SortedSet;19import java.util.TreeMap;20import java.util.TreeSet;21import org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues;22import org.mockito.internal.util.JavaEightUtil;23import org.mockito.internal.util.Primitives;24import org.mockito.invocation.InvocationOnMock;25import io.smallrye.mutiny.Multi;26import io.smallrye.mutiny.Uni;27@SuppressWarnings("serial")28public class MutinyAnswer extends ReturnsEmptyValues {29 @Override30 public Object answer(InvocationOnMock inv) {31 if (isToStringMethod(inv.getMethod())32 || isCompareToMethod(inv.getMethod())) {33 return super.answer(inv);34 }35 Class<?> returnType = inv.getMethod().getReturnType();36 // save the user some time figuring out this issue when it happens37 if ((returnType.getName().equals(Multi.class.getName()) && returnType != Multi.class)38 || (returnType.getName().equals(Uni.class.getName()) && returnType != Uni.class)) {39 throw new IllegalStateException("Class loader issue: we have two Multi classes with different class loaders. "40 + "Make sure to initialize this class with the QuarkusClassLoader.");41 }42 if (returnType == Multi.class) {43 return Multi.createFrom().item(returnValueForMutiny(inv.getMethod().getGenericReturnType()));44 } else if (returnType == Uni.class) {45 return Uni.createFrom().item(returnValueForMutiny(inv.getMethod().getGenericReturnType()));46 }47 return returnValueForClass(returnType);48 }49 private Object returnValueForMutiny(Type uniOrMultiType) {50 // check for raw types51 if (uniOrMultiType instanceof Class)52 return returnValueForClass(Object.class);53 Type ret = ((ParameterizedType) uniOrMultiType).getActualTypeArguments()[0];54 return returnValueForType(ret);55 }56 private Object returnValueForType(Type type) {57 if (type instanceof Class)58 return returnValueForClass((Class<?>) type);59 if (type instanceof ParameterizedType)60 return returnValueForClass((Class<?>) ((ParameterizedType) type).getRawType());61 if (type instanceof TypeVariable) {62 TypeVariable<?> tv = (TypeVariable<?>) type;63 // default upper bound is Object so we always have a value64 return returnValueForType(tv.getBounds()[0]);65 }66 return returnValueForClass(Object.class);67 }68 // copied from supertype due to access restriction :(69 Object returnValueForClass(Class<?> type) {70 if (Primitives.isPrimitiveOrWrapper(type)) {71 return Primitives.defaultValue(type);72 //new instances are used instead of Collections.emptyList(), etc.73 //to avoid UnsupportedOperationException if code under test modifies returned collection74 } else if (type == Iterable.class) {75 return new ArrayList<Object>(0);76 } else if (type == Collection.class) {77 return new LinkedList<Object>();78 } else if (type == Set.class) {79 return new HashSet<Object>();80 } else if (type == HashSet.class) {81 return new HashSet<Object>();82 } else if (type == SortedSet.class) {83 return new TreeSet<Object>();84 } else if (type == TreeSet.class) {85 return new TreeSet<Object>();86 } else if (type == LinkedHashSet.class) {87 return new LinkedHashSet<Object>();88 } else if (type == List.class) {89 return new LinkedList<Object>();90 } else if (type == LinkedList.class) {91 return new LinkedList<Object>();92 } else if (type == ArrayList.class) {93 return new ArrayList<Object>();94 } else if (type == Map.class) {95 return new HashMap<Object, Object>();96 } else if (type == HashMap.class) {97 return new HashMap<Object, Object>();98 } else if (type == SortedMap.class) {99 return new TreeMap<Object, Object>();100 } else if (type == TreeMap.class) {101 return new TreeMap<Object, Object>();102 } else if (type == LinkedHashMap.class) {103 return new LinkedHashMap<Object, Object>();104 } else if ("java.util.Optional".equals(type.getName())) {105 return JavaEightUtil.emptyOptional();106 } else if ("java.util.OptionalDouble".equals(type.getName())) {107 return JavaEightUtil.emptyOptionalDouble();108 } else if ("java.util.OptionalInt".equals(type.getName())) {109 return JavaEightUtil.emptyOptionalInt();110 } else if ("java.util.OptionalLong".equals(type.getName())) {111 return JavaEightUtil.emptyOptionalLong();112 } else if ("java.util.stream.Stream".equals(type.getName())) {113 return JavaEightUtil.emptyStream();114 } else if ("java.util.stream.DoubleStream".equals(type.getName())) {115 return JavaEightUtil.emptyDoubleStream();116 } else if ("java.util.stream.IntStream".equals(type.getName())) {117 return JavaEightUtil.emptyIntStream();118 } else if ("java.util.stream.LongStream".equals(type.getName())) {119 return JavaEightUtil.emptyLongStream();120 }121 //Let's not care about the rest of collections.122 return null;123 }124}...

Full Screen

Full Screen

JavaEightUtil

Using AI Code Generation

copy

Full Screen

1JavaEightUtil.isOptionalPresent(Optional.of("abc"));2JavaEightUtil.isOptionalPresent(Optional.empty());3JavaEightUtil.isOptionalPresent(Optional.ofNullable(null));4JavaEightUtil.isOptionalPresent(Optional.ofNullable("abc"));5JavaEightUtil.isOptionalPresent(null);6JavaEightUtil.isOptionalPresent("abc");7JavaEightUtil.isOptionalPresent(Optional.of("abc"));8JavaEightUtil.isOptionalPresent(Optional.empty());9JavaEightUtil.isOptionalPresent(Optional.ofNullable(null));10JavaEightUtil.isOptionalPresent(Optional.ofNullable("abc"));11JavaEightUtil.isOptionalPresent(null);12JavaEightUtil.isOptionalPresent("abc");13JavaEightUtil.isOptionalPresent(Optional.of("abc"));14JavaEightUtil.isOptionalPresent(Optional.empty());15JavaEightUtil.isOptionalPresent(Optional.ofNullable(null));

Full Screen

Full Screen

JavaEightUtil

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.util;2import java.util.function.Supplier;3public class JavaEightUtil {4 public static <T> T orElseGet(Supplier<T> supplier, T defaultValue) {5 return defaultValue;6 }7}8package org.mockito.internal.util;9import java.util.function.Supplier;10public class JavaEightUtil {11 public static <T> T orElseGet(Supplier<T> supplier, T defaultValue) {12 return supplier.get();13 }14}15package org.mockito.internal.util;16import java.util.function.Supplier;17public class JavaEightUtil {18 public static <T> T orElseGet(Supplier<T> supplier, T defaultValue) {19 return supplier.get();20 }21}22package org.mockito.internal.util;23import java.util.function.Supplier;24public class JavaEightUtil {25 public static <T> T orElseGet(Supplier<T> supplier, T defaultValue) {26 return supplier.get();27 }28}29package org.mockito.internal.util;30import java.util.function.Supplier;31public class JavaEightUtil {32 public static <T> T orElseGet(Supplier<T> supplier, T defaultValue) {33 return supplier.get();34 }35}36package org.mockito.internal.util;37import java.util.function.Supplier;38public class JavaEightUtil {39 public static <T> T orElseGet(Supplier<T> supplier, T defaultValue) {40 return supplier.get();41 }42}43package org.mockito.internal.util;44import java.util.function.Supplier;45public class JavaEightUtil {46 public static <T> T orElseGet(Supplier<T> supplier, T defaultValue) {47 return supplier.get();48 }49}50package org.mockito.internal.util;51import java.util.function.Supplier;52public class JavaEightUtil {53 public static <T> T orElseGet(Supplier<T> supplier, T defaultValue) {54 return supplier.get();55 }56}57package org.mockito.internal.util;58import java.util.function.Supplier;59public class JavaEightUtil {60 public static <T> T orElseGet(Supplier<T> supplier, T defaultValue) {61 return supplier.get();62 }63}64package org.mockito.internal.util;65import java.util.function.Supplier;

Full Screen

Full Screen

JavaEightUtil

Using AI Code Generation

copy

Full Screen

1package org.mockito;2import org.mockito.internal.util.JavaEightUtil;3public class JavaEightUtilTest {4 public static void main(String[] args) {5 JavaEightUtil.isJavaEightOrHigher();6 }7}

Full Screen

Full Screen

JavaEightUtil

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.util.JavaEightUtil;2class Test {3 public static void main(String[] args) {4 System.out.println("Is Java8 or later? " + JavaEightUtil.isJava8OrLater());5 System.out.println("Is Java8 or later? " + JavaEightUtil.isJava8OrLater());6 }7}

Full Screen

Full Screen

JavaEightUtil

Using AI Code Generation

copy

Full Screen

1package org.mockito.internal.util;2import java.lang.reflect.Method;3import java.util.concurrent.Callable;4import java.util.concurrent.ExecutionException;5import java.util.concurrent.FutureTask;6public class JavaEightUtil {7 private static final boolean IS_JAVA_8;8 static {9 IS_JAVA_8 = isJava8();10 }11 public static boolean isJava8() {12 try {13 Class<?> javaUtilOptionalClass = Class.forName("java.util.Optional");14 Method ofNullableMethod = javaUtilOptionalClass.getMethod("ofNullable", Object.class);15 FutureTask<Boolean> futureTask = new FutureTask<Boolean>(new Callable<Boolean>() {16 public Boolean call() throws Exception {17 return ofNullableMethod != null;18 }19 });20 new Thread(futureTask).start();21 return futureTask.get();22 } catch (ClassNotFoundException e) {23 return false;24 } catch (NoSuchMethodException e) {25 return false;26 } catch (InterruptedException e) {27 return false;28 } catch (ExecutionException e) {29 return false;30 }31 }32 public static boolean isJava8OrHigher() {33 return IS_JAVA_8;34 }35}36package org.mockito.internal.util;37import java.util.Optional;38import java.util.function.Consumer;39import java.util.function.Function;40import java.util.function.Predicate;41import java.util.function.Supplier;42public class JavaEightUtil {43 private static final boolean IS_JAVA_8;44 static {45 IS_JAVA_8 = isJava8();46 }47 public static boolean isJava8() {48 try {49 Class<?> javaUtilOptionalClass = Class.forName("java.util.Optional");50 Method ofNullableMethod = javaUtilOptionalClass.getMethod("ofNullable", Object.class);51 FutureTask<Boolean> futureTask = new FutureTask<Boolean>(new Callable<Boolean>() {52 public Boolean call() throws Exception {53 return ofNullableMethod != null;54 }55 });56 new Thread(futureTask).start();57 return futureTask.get();58 } catch (ClassNotFoundException e) {59 return false;60 } catch (NoSuchMethodException e) {61 return false;62 } catch (InterruptedException e) {63 return false;64 } catch (ExecutionException e) {65 return false;66 }67 }68 public static boolean isJava8OrHigher() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful