How to use GenericMetadataSupport class of org.mockito.internal.util.reflection package

Best Mockito code snippet using org.mockito.internal.util.reflection.GenericMetadataSupport

Source:ReturnsDeepStubs.java Github

copy

Full Screen

...10import org.mockito.internal.creation.settings.CreationSettings;11import org.mockito.internal.stubbing.InvocationContainerImpl;12import org.mockito.internal.stubbing.StubbedInvocationMatcher;13import org.mockito.internal.util.MockUtil;14import org.mockito.internal.util.reflection.GenericMetadataSupport;15import org.mockito.invocation.InvocationOnMock;16import org.mockito.stubbing.Answer;1718import java.io.Serializable;1920import static org.mockito.Mockito.withSettings;2122/**23 * Returning deep stub implementation.24 *25 * Will return previously created mock if the invocation matches.26 *27 * <p>Supports nested generic information, with this answer you can write code like this :28 *29 * <pre class="code"><code class="java">30 * interface GenericsNest&lt;K extends Comparable&lt;K&gt; & Cloneable&gt; extends Map&lt;K, Set&lt;Number&gt;&gt; {}31 *32 * GenericsNest&lt;?&gt; mock = mock(GenericsNest.class, new ReturnsGenericDeepStubs());33 * Number number = mock.entrySet().iterator().next().getValue().iterator().next();34 * </code></pre>35 * </p>36 *37 * @see org.mockito.Mockito#RETURNS_DEEP_STUBS38 * @see org.mockito.Answers#RETURNS_DEEP_STUBS39 */40public class ReturnsDeepStubs implements Answer<Object>, Serializable {41 42 private static final long serialVersionUID = -7105341425736035847L;4344 private MockitoCore mockitoCore = new MockitoCore();45 private ReturnsEmptyValues delegate = new ReturnsEmptyValues();4647 public Object answer(InvocationOnMock invocation) throws Throwable {48 GenericMetadataSupport returnTypeGenericMetadata =49 actualParameterizedType(invocation.getMock()).resolveGenericReturnType(invocation.getMethod());5051 Class<?> rawType = returnTypeGenericMetadata.rawType();52 if (!mockitoCore.isTypeMockable(rawType)) {53 return delegate.returnValueFor(rawType);54 }5556 return getMock(invocation, returnTypeGenericMetadata);57 }5859 private Object getMock(InvocationOnMock invocation, GenericMetadataSupport returnTypeGenericMetadata) throws Throwable {60 InternalMockHandler<Object> handler = new MockUtil().getMockHandler(invocation.getMock());61 InvocationContainerImpl container = (InvocationContainerImpl) handler.getInvocationContainer();6263 // matches invocation for verification64 for (StubbedInvocationMatcher stubbedInvocationMatcher : container.getStubbedInvocations()) {65 if(container.getInvocationForStubbing().matches(stubbedInvocationMatcher.getInvocation())) {66 return stubbedInvocationMatcher.answer(invocation);67 }68 }6970 // deep stub71 return recordDeepStubMock(createNewDeepStubMock(returnTypeGenericMetadata), container);72 }7374 /**75 * Creates a mock using the Generics Metadata.76 *77 * <li>Finally as we want to mock the actual type, but we want to pass along the contextual generics meta-data78 * that was resolved for the current return type, for this to happen we associate to the mock an new instance of79 * {@link ReturnsDeepStubs} answer in which we will store the returned type generic metadata.80 *81 * @param returnTypeGenericMetadata The metadata to use to create the new mock.82 * @return The mock83 */84 private Object createNewDeepStubMock(GenericMetadataSupport returnTypeGenericMetadata) {85 return mockitoCore.mock(86 returnTypeGenericMetadata.rawType(),87 withSettingsUsing(returnTypeGenericMetadata)88 );89 }9091 private MockSettings withSettingsUsing(GenericMetadataSupport returnTypeGenericMetadata) {92 MockSettings mockSettings =93 returnTypeGenericMetadata.rawExtraInterfaces().length > 0 ?94 withSettings().extraInterfaces(returnTypeGenericMetadata.rawExtraInterfaces())95 : withSettings();9697 return mockSettings98 .defaultAnswer(returnsDeepStubsAnswerUsing(returnTypeGenericMetadata));99 }100101 private ReturnsDeepStubs returnsDeepStubsAnswerUsing(final GenericMetadataSupport returnTypeGenericMetadata) {102 return new ReturnsDeepStubs() {103 @Override104 protected GenericMetadataSupport actualParameterizedType(Object mock) {105 return returnTypeGenericMetadata;106 }107 };108 }109110 private Object recordDeepStubMock(final Object mock, InvocationContainerImpl container) throws Throwable {111112 container.addAnswer(new Answer<Object>() {113 public Object answer(InvocationOnMock invocation) throws Throwable {114 return mock;115 }116 }, false);117118 return mock;119 }120121 protected GenericMetadataSupport actualParameterizedType(Object mock) {122 CreationSettings mockSettings = (CreationSettings) new MockUtil().getMockHandler(mock).getMockSettings();123 return GenericMetadataSupport.inferFrom(mockSettings.getTypeToMock());124 }125} ...

Full Screen

Full Screen

GenericMetadataSupport

Using AI Code Generation

copy

Full Screen

1 public static Object getMetadata(Object target, String metadataName) {2 Class<?> targetClass = target.getClass();3 Object metadata = null;4 try {5 metadata = GenericMetadataSupport.getMetadata(targetClass, metadataName);6 } catch (Exception e) {7 }8 return metadata;9 }10}11public static Object getMetadata(Object target, String metadataName) {12 Class<?> targetClass = target.getClass();13 Object metadata = null;14 try {15 metadata = GenericMetadataSupport.getMetadata(targetClass, metadataName);16 } catch (Exception e) {17 }18 return metadata;19 }20public static Object getMetadata(Object target, String metadataName) {21 Class<?> targetClass = target.getClass();22 Object metadata = null;23 try {24 metadata = GenericMetadataSupport.getMetadata(targetClass, metadataName);25 } catch (Exception e) {26 }27 return metadata;28 }29public static Object getMetadata(Object target, String metadataName) {30 Class<?> targetClass = target.getClass();31 Object metadata = null;32 try {

Full Screen

Full Screen

GenericMetadataSupport

Using AI Code Generation

copy

Full Screen

1Mockito.mockingDetails(someClass).getMockCreationSettings().getMockName();2Mockito.mockingDetails(someMock).getMockCreationSettings().getMockName();3Mockito.mockingDetails(someClass).getMockCreationSettings().getExtraInterfaces();4Mockito.mockingDetails(someMock).getMockCreationSettings().getExtraInterfaces();5Mockito.mockingDetails(someClass).getMockCreationSettings().getSerializableMode();6Mockito.mockingDetails(someMock).getMockCreationSettings().getSerializableMode();7Mockito.mockingDetails(someClass).getMockCreationSettings().getDefaultAnswer();8Mockito.mockingDetails(someMock).getMockCreationSettings().getDefaultAnswer();9Mockito.mockingDetails(someClass).getMockCreationSettings().getInvocationListeners();10Mockito.mockingDetails(someMock).getMockCreationSettings().getInvocationListeners();11Mockito.mockingDetails(someClass).getMockCreationSettings().getStubbableOngoingMethods();12Mockito.mockingDetails(someMock).getMockCreationSettings().getStubb

Full Screen

Full Screen

GenericMetadataSupport

Using AI Code Generation

copy

Full Screen

1import org.mockito.internal.util.reflection.GenericMetadataSupport;2import org.mockito.internal.util.reflection.GenericMetadataSupport.GenericType;3public class TestMockitoGenericType {4 public static void main(String[] args) {5 GenericType genericType = GenericMetadataSupport.getGenericReturnType(6 TestMockitoGenericType.class.getDeclaredMethods()[0]);7 System.out.println(genericType);8 }9 public List<String> getNames() {10 return null;11 }12}13import org.mockito.internal.util.reflection.GenericMetadataSupport;14import org.mockito.internal.util.reflection.GenericMetadataSupport.GenericType;15public class TestMockitoGenericType {16 public static void main(String[] args) {17 GenericType genericType = GenericMetadataSupport.getGenericReturnType(18 TestMockitoGenericType.class.getDeclaredMethods()[0]);19 System.out.println(genericType);20 }21 public List<String> getNames() {22 return null;23 }24}

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