How to use NoMockito method of org.powermock.core.MockGateway class

Best Powermock code snippet using org.powermock.core.MockGateway.NoMockito

Source:MockGateway.java Github

copy

Full Screen

...30 /**31 * {@link #noMockito} is wrapped into it's own static class to make sure it is initialized not earlier than32 * {@link #calledFromMockito()} is called for the first time.33 */34 private static final class NoMockito {35 static final boolean noMockito = Package.getPackage("org.mockito") == null;36 private NoMockito() {}37 }38 public static final Object PROCEED = new Object();39 public static final Object SUPPRESS = new Object();40 /**41 * Used to tell the MockGateway that the next call should not be mocked42 * regardless if a {@link MethodInvocationControl} is found in the43 * {@link MockRepository}. Used to allow for e.g. recursive partial mocking.44 */45 public static final String DONT_MOCK_NEXT_CALL = "DontMockNextCall";46 /**47 * Tells PowerMock to mock standard methods. These are48 * {@link Object#toString()}, {@link Object#hashCode()} and49 * {@link Object#equals(Object)}. By default this is {@code true}.50 */51 public static boolean MOCK_STANDARD_METHODS = true;52 @SuppressWarnings("UnusedDeclaration")53 public static Object newInstanceCall(Class<?> type, Object[] args, Class<?>[] sig) throws Throwable {54 final NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type);55 if (newInvocationControl != null) {56 /*57 * We need to deal with inner, local and anonymous inner classes58 * specifically. For example when new is invoked on an inner class59 * it seems like null is passed as an argument even though it60 * shouldn't. We correct this here.61 *62 * Seems with Javassist 3.17.1-GA & Java 7, the 'null' is passed as the last argument.63 */64 if (type.isMemberClass() && Modifier.isStatic(type.getModifiers())) {65 if (args.length > 066 && ((args[0] == null && sig[0].getCanonicalName() == null)67 || (args[args.length - 1] == null && sig[args.length - 1].getCanonicalName() == null))68 && sig.length > 0) {69 args = copyArgumentsForInnerOrLocalOrAnonymousClass(args, sig[0], false);70 }71 } else if (type.isLocalClass() || type.isAnonymousClass() || type.isMemberClass()) {72 if (args.length > 0 && sig.length > 0 && sig[0].equals(type.getEnclosingClass())) {73 args = copyArgumentsForInnerOrLocalOrAnonymousClass(args, sig[0], true);74 }75 }76 return newInvocationControl.invoke(type, args, sig);77 }78 // Check if we should suppress the constructor code79 if (MockRepository.shouldSuppressConstructor(WhiteboxImpl.getConstructor(type, sig))) {80 return WhiteboxImpl.getFirstParentConstructor(type);81 }82 return PROCEED;83 }84 @SuppressWarnings("UnusedDeclaration")85 public static Object fieldCall(Object instanceOrClassContainingTheField, Class<?> classDefiningField,86 String fieldName, Class<?> fieldType) {87 if (MockRepository.shouldSuppressField(WhiteboxImpl.getField(classDefiningField, fieldName))) {88 return TypeUtils.getDefaultValue(fieldType);89 }90 return PROCEED;91 }92 public static Object staticConstructorCall(String className) {93 if (MockRepository.shouldSuppressStaticInitializerFor(className)) {94 return "suppress";95 }96 return PROCEED;97 }98 @SuppressWarnings("UnusedDeclaration")99 public static Object constructorCall(Class<?> type, Object[] args, Class<?>[] sig) throws Throwable {100 final Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig);101 if (MockRepository.shouldSuppressConstructor(constructor)) {102 return null;103 }104 return PROCEED;105 }106 107 public static boolean suppressConstructorCall(Class<?> type, Object[] args, Class<?>[] sig) throws Throwable {108 return constructorCall(type, args, sig) != PROCEED;109 }110 /**111 * Tells PowerMock whether or not to mock112 * {@link java.lang.Object#getClass()}.113 */114 public static boolean MOCK_GET_CLASS_METHOD = false;115 /**116 * Tells PowerMock whether or not to mock117 * {@link java.lang.Class#isAnnotationPresent(Class)} and118 * {@link java.lang.Class#getAnnotation(Class)}.119 */120 public static boolean MOCK_ANNOTATION_METHODS = false;121 // used for instance methods122 @SuppressWarnings("UnusedDeclaration")123 public static Object methodCall(Object instance, String methodName, Object[] args, Class<?>[] sig,124 String returnTypeAsString) throws Throwable {125 return doMethodCall(instance, methodName, args, sig, returnTypeAsString);126 }127 // used for static methods128 @SuppressWarnings("UnusedDeclaration")129 public static Object methodCall(Class<?> type, String methodName, Object[] args, Class<?>[] sig,130 String returnTypeAsString) throws Throwable {131 return doMethodCall(type, methodName, args, sig, returnTypeAsString);132 }133 private static Object doMethodCall(Object object, String methodName, Object[] args, Class<?>[] sig,134 String returnTypeAsString) throws Throwable {135 if (!shouldMockMethod(methodName, sig)) {136 return PROCEED;137 }138 MockInvocation mockInvocation = new MockInvocation(object, methodName, sig);139 MethodInvocationControl methodInvocationControl = mockInvocation.getMethodInvocationControl();140 Object returnValue = null;141 // The following describes the equals non-static method.142 if (isEqualsMethod(mockInvocation) && !isStaticMethod(mockInvocation)) {143 returnValue = tryHandleEqualsMethod(mockInvocation);144 }145 if (returnValue != null) {146 return returnValue;147 }148 return doMethodCall(object, args, returnTypeAsString, mockInvocation, methodInvocationControl);149 }150 private static Object doMethodCall(Object object, Object[] args,151 String returnTypeAsString,152 MockInvocation mockInvocation,153 MethodInvocationControl methodInvocationControl) throws Throwable {154 Object returnValue;155 // At first should be checked that method not suppressed/stubbed, because otherwise for spies real156 // method is involved.157 // https://github.com/jayway/powermock/issues/327158 if (MockRepository.shouldSuppressMethod(mockInvocation.getMethod(), mockInvocation.getObjectType())) {159 returnValue = TypeUtils.getDefaultValue(returnTypeAsString);160 } else if (MockRepository.shouldStubMethod(mockInvocation.getMethod())) {161 returnValue = MockRepository.getMethodToStub(mockInvocation.getMethod());162 } else if (methodInvocationControl != null && methodInvocationControl.isMocked(mockInvocation.getMethod()) && shouldMockThisCall()) {163 returnValue = methodInvocationControl.invoke(object, mockInvocation.getMethod(), args);164 if (returnValue == SUPPRESS) {165 returnValue = TypeUtils.getDefaultValue(returnTypeAsString);166 }167 } else if (MockRepository.hasMethodProxy(mockInvocation.getMethod())) {168 /*169 * We must temporary remove the method proxy when invoking the170 * invocation handler because if the invocation handler delegates171 * the call we will end up here again and we'll get a172 * StackOverflowError.173 */174 final InvocationHandler invocationHandler = MockRepository.removeMethodProxy(mockInvocation.getMethod());175 try {176 returnValue = invocationHandler.invoke(object, mockInvocation.getMethod(), args);177 } finally {178 // Set the method proxy again after the invocation179 MockRepository.putMethodProxy(mockInvocation.getMethod(), invocationHandler);180 }181 } else {182 returnValue = PROCEED;183 }184 return returnValue;185 }186 /*187 * Method handles exception cases with equals method.188 */189 private static Object tryHandleEqualsMethod(MockInvocation mockInvocation) {190 // Fix for Issue http://code.google.com/p/powermock/issues/detail?id=88191 // For some reason the method call to equals() on final methods is192 // intercepted and during the further processing in Mockito the same193 // equals() method is called on the same instance. A StackOverflowError194 // is the result. The following fix changes this by checking if the195 // method to be called is a final equals() method. In that case the196 // original method is called by returning PROCEED.197 if (mockInvocation.getMethod().getParameterTypes().length == 1198 && mockInvocation.getMethod().getParameterTypes()[0] == Object.class199 && Modifier.isFinal(mockInvocation.getMethod().getModifiers())) {200 return PROCEED;201 }202 if (calledFromMockito()){203 return PROCEED;204 }205 return null;206 }207 private static boolean isEqualsMethod(MockInvocation mockInvocation) {208 return "equals".equals(mockInvocation.getMethod().getName());209 }210 private static boolean isStaticMethod(MockInvocation mockInvocation) {211 return Modifier.isStatic(mockInvocation.getMethod().getModifiers());212 }213 private static boolean calledFromMockito() {214 if (NoMockito.noMockito) {215 return false;216 }217 StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();218 for (StackTraceElement stackTraceElement : stackTrace) {219 if (stackTraceElement.getClassName().startsWith("org.mockito.")){220 return true;221 }222 }223 return false;224 }225 private static boolean shouldMockMethod(String methodName, Class<?>[] sig) {226 if (isJavaStandardMethod(methodName, sig) && !MOCK_STANDARD_METHODS) {227 return false;228 } else if (isGetClassMethod(methodName, sig) && !MOCK_GET_CLASS_METHOD) {...

Full Screen

Full Screen

NoMockito

Using AI Code Generation

copy

Full Screen

1The following code snippet shows the usage of PowerMockito.mockStatic() method:2The following code snippet shows the usage of PowerMockito.mock() method:3The following code snippet shows the usage of PowerMockito.when() method:4The following code snippet shows the usage of PowerMockito.doReturn() method:5The following code snippet shows the usage of PowerMockito.doThrow() method:6The following code snippet shows the usage of PowerMockito.doAnswer() method:7The following code snippet shows the usage of PowerMockito.verifyStatic() method:8The following code snippet shows the usage of PowerMockito.verifyPrivate() method:9The following code snippet shows the usage of PowerMockito.verify() method:10The following code snippet shows the usage of PowerMockito.spy() method:11The following code snippet shows the usage of PowerMockito.verifyNoMoreInteractions() method:12The following code snippet shows the usage of PowerMockito.verifyNoMoreInteractions() method:13The following code snippet shows the usage of PowerMockito.verifyZeroInteractions() method:14The following code snippet shows the usage of PowerMockito.verifyNoInteractions() method:

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