How to use annotateClass method of org.mockito.internal.creation.bytebuddy.MockMethodInterceptor class

Best Mockito code snippet using org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.annotateClass

Source:ByteBuddyCrossClassLoaderSerializationSupport.java Github

copy

Full Screen

...212 * This specifically crafted ObjectInoutStream has the most important role to resolve the Mockito generated213 * class. It is doing so via the {@link #resolveClass(ObjectStreamClass)} which looks in the stream214 * for a Mockito marker. If this marker is found it will try to resolve the mockito class otherwise it215 * delegates class resolution to the default super behavior.216 * The mirror method used for serializing the mock is {@link MockitoMockObjectOutputStream#annotateClass(Class)}.217 * </p>218 * <p/>219 * <p>220 * When this marker is found, {@link ByteBuddyMockMaker#createProxyClass(MockFeatures)} methods are being used221 * to create the mock class.222 * </p>223 */224 public static class MockitoMockObjectInputStream extends ObjectInputStream {225 private final Class typeToMock;226 private final Set<Class> extraInterfaces;227 public MockitoMockObjectInputStream(InputStream in, Class typeToMock, Set<Class> extraInterfaces) throws IOException {228 super(in);229 this.typeToMock = typeToMock;230 this.extraInterfaces = extraInterfaces;231 enableResolveObject(true); // ensure resolving is enabled232 }233 /**234 * Resolve the Mockito proxy class if it is marked as such.235 * <p/>236 * <p>Uses the fields {@link #typeToMock} and {@link #extraInterfaces} to237 * create the Mockito proxy class as the <code>ObjectStreamClass</code>238 * doesn't carry useful information for this purpose.</p>239 *240 * @param desc Description of the class in the stream, not used.241 * @return The class that will be used to deserialize the instance mock.242 * @throws java.io.IOException243 * @throws ClassNotFoundException244 */245 @Override246 protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {247 if (notMarkedAsAMockitoMock(readObject())) {248 return super.resolveClass(desc);249 }250 // TODO check the class is mockable in the deserialization side251 // create the Mockito mock class before it can even be deserialized252 Class<?> proxyClass = ((ByteBuddyMockMaker) Plugins.getMockMaker())253 .createProxyClass(withMockFeatures(typeToMock, extraInterfaces, true));254 hackClassNameToMatchNewlyCreatedClass(desc, proxyClass);255 return proxyClass;256 }257 /**258 * Hack the <code>name</code> field of the given <code>ObjectStreamClass</code> with259 * the <code>newProxyClass</code>.260 * <p/>261 * The parent ObjectInputStream will check the name of the class in the stream matches the name of the one262 * that is created in this method.263 * <p/>264 * The CGLIB classes uses a hash of the classloader and/or maybe some other data that allow them to be265 * relatively unique in a JVM.266 * <p/>267 * When names differ, which happens when the mock is deserialized in another ClassLoader, a268 * <code>java.io.InvalidObjectException</code> is thrown, so this part of the code is hacking through269 * the given <code>ObjectStreamClass</code> to change the name with the newly created class.270 *271 * @param descInstance The <code>ObjectStreamClass</code> that will be hacked.272 * @param proxyClass The proxy class whose name will be applied.273 * @throws java.io.InvalidObjectException274 */275 private void hackClassNameToMatchNewlyCreatedClass(ObjectStreamClass descInstance, Class<?> proxyClass) throws ObjectStreamException {276 try {277 Field classNameField = descInstance.getClass().getDeclaredField("name");278 new FieldSetter(descInstance, classNameField).set(proxyClass.getCanonicalName());279 } catch (NoSuchFieldException nsfe) {280 throw new MockitoSerializationIssue(join(281 "Wow, the class 'ObjectStreamClass' in the JDK don't have the field 'name',",282 "this is definitely a bug in our code as it means the JDK team changed a few internal things.",283 "",284 "Please report an issue with the JDK used, a code sample and a link to download the JDK would be welcome."285 ), nsfe);286 }287 }288 /**289 * Read the stream class annotation and identify it as a Mockito mock or not.290 *291 * @param marker The marker to identify.292 * @return <code>true</code> if not marked as a Mockito, <code>false</code> if the class annotation marks a Mockito mock.293 * @throws java.io.IOException294 * @throws ClassNotFoundException295 */296 private boolean notMarkedAsAMockitoMock(Object marker) throws IOException, ClassNotFoundException {297 return !MOCKITO_PROXY_MARKER.equals(marker);298 }299 }300 /**301 * Special Mockito aware <code>ObjectOutputStream</code>.302 * <p/>303 * <p>304 * This output stream has the role of marking in the stream the Mockito class. This305 * marking process is necessary to identify the proxy class that will need to be recreated.306 * <p/>307 * The mirror method used for deserializing the mock is308 * {@link MockitoMockObjectInputStream#resolveClass(ObjectStreamClass)}.309 * </p>310 */311 private static class MockitoMockObjectOutputStream extends ObjectOutputStream {312 private static final String NOTHING = "";313 public MockitoMockObjectOutputStream(ByteArrayOutputStream out) throws IOException {314 super(out);315 }316 /**317 * Annotates (marks) the class if this class is a Mockito mock.318 *319 * @param cl The class to annotate.320 * @throws java.io.IOException321 */322 @Override323 protected void annotateClass(Class<?> cl) throws IOException {324 writeObject(mockitoProxyClassMarker(cl));325 // might be also useful later, for embedding classloader info ...maybe ...maybe not326 }327 /**328 * Returns the Mockito marker if this class is a Mockito mock.329 *330 * @param cl The class to mark.331 * @return The marker if this is a Mockito proxy class, otherwise returns a void marker.332 */333 private String mockitoProxyClassMarker(Class<?> cl) {334 if (CrossClassLoaderSerializableMock.class.isAssignableFrom(cl)) {335 return MOCKITO_PROXY_MARKER;336 } else {337 return NOTHING;...

Full Screen

Full Screen

annotateClass

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito2import org.mockito.internal.creation.bytebuddy.MockMethodInterceptor3import org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.*4import org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator5import org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.*6import org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator7import org.mockito.internal.creation.bytebuddy.InlineBytecodeGenerator.*8import org.mockito.internal.creation.bytebuddy.MockFeatures9import org.mockito.internal.creation.bytebuddy.MockFeatures.*10import org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator11import org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.*12import org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.CacheMode.*13import org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.TypeMockability.*14import org.mockito.internal.creation.bytebuddy.SubclassBytecodeGenerator.TypeMockability.*15import org.mockito.internal.creation.bytebuddy.TypeCachingBytecodeGenerator.CacheMode.*16import org.mockito.internal.creation.bytebuddy.TypeCachingB

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