当前位置: 首页>>代码示例>>Java>>正文


Java MockUtil.getMockName方法代码示例

本文整理汇总了Java中org.mockito.internal.util.MockUtil.getMockName方法的典型用法代码示例。如果您正苦于以下问题:Java MockUtil.getMockName方法的具体用法?Java MockUtil.getMockName怎么用?Java MockUtil.getMockName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.mockito.internal.util.MockUtil的用法示例。


在下文中一共展示了MockUtil.getMockName方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getMocksWithNames

import org.mockito.internal.util.MockUtil; //导入方法依赖的package包/类
private Map<String, Object> getMocksWithNames() {
    Map<String, Object> result = new HashMap<String, Object>();
    MockUtil mockUtil = new MockUtil();
    for (Object mock : getMocks()) {
        MockName mockName = mockUtil.getMockName(mock);
        result.put(mockName.toString(), mock);
    }

    return result;
}
 
开发者ID:mp911de,项目名称:jee-commons,代码行数:11,代码来源:MockitoLookupStrategy.java

示例2: toString

import org.mockito.internal.util.MockUtil; //导入方法依赖的package包/类
@Override public String toString() {
    MockUtil mockUtil = new MockUtil();
    return "HashCodeAndEqualsMockWrapper{" +
            "mockInstance=" + (mockUtil.isMock(mockInstance) ? mockUtil.getMockName(mockInstance) : typeInstanceString()) +
            '}';
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:7,代码来源:HashCodeAndEqualsMockWrapper.java

示例3: writeReplace

import org.mockito.internal.util.MockUtil; //导入方法依赖的package包/类
/**
 * Custom implementation of the <code>writeReplace</code> method for serialization.
 * <p/>
 * Here's how it's working and why :
 * <ol>
 * <li>
 * <p>When first entering in this method, it's because some is serializing the mock, with some code like :
 * <pre class="code"><code class="java">
 * objectOutputStream.writeObject(mock);
 * </code></pre>
 *         So, {@link ObjectOutputStream} will track the <code>writeReplace</code> method in the instance and
 *         execute it, which is wanted to replace the mock by another type that will encapsulate the actual mock.
 *         At this point, the code will return an
 *         {@link AcrossJVMSerializationFeature.AcrossJVMMockSerializationProxy}.</p>
 *     </li>
 *     <li>
 *         <p>Now, in the constructor
 *         {@link AcrossJVMSerializationFeature.AcrossJVMMockSerializationProxy#AcrossJVMMockSerializationProxy(Object)}
 *         the mock is being serialized in a custom way (using
 *         {@link AcrossJVMSerializationFeature.MockitoMockObjectOutputStream}) to a
 *         byte array. So basically it means the code is performing double nested serialization of the passed
 *         <code>mockitoMock</code>.</p>
 *
 *         <p>However the <code>ObjectOutputStream</code> will still detect the custom
 *         <code>writeReplace</code> and execute it.
 *         <em>(For that matter disabling replacement via {@link ObjectOutputStream#enableReplaceObject(boolean)}
 *         doesn't disable the <code>writeReplace</code> call, but just just toggle replacement in the
 *         written stream, <strong><code>writeReplace</code> is always called by
 *         <code>ObjectOutputStream</code></strong>.)</em></p>
 *
 *         <p>In order to avoid this recursion, obviously leading to a {@link StackOverflowError}, this method is using
 *         a flag that marks the mock as already being replaced, and then shouldn't replace itself again.
 *         <strong>This flag is local to this class</strong>, which means the flag of this class unfortunately needs
 *         to be protected against concurrent access, hence the reentrant lock.</p>
 *     </li>
 * </ol>
 *
 * @param mockitoMock The Mockito mock to be serialized.
 * @return A wrapper ({@link AcrossJVMMockSerializationProxy}) to be serialized by the calling ObjectOutputStream.
 * @throws ObjectStreamException
 */
public Object writeReplace(Object mockitoMock) throws ObjectStreamException {
    try {
        // reentrant lock for critical section. could it be improved ?
        mutex.lock();
        // mark started flag // per thread, not per instance
        // temporary loosy hack to avoid stackoverflow
        if (mockIsCurrentlyBeingReplaced()) {
            return mockitoMock;
        }
        mockReplacementStarted();

        return new AcrossJVMMockSerializationProxy(mockitoMock);
    } catch (IOException ioe) {
        MockUtil mockUtil = new MockUtil();
        MockName mockName = mockUtil.getMockName(mockitoMock);
        String mockedType = mockUtil.getMockSettings(mockitoMock).getTypeToMock().getCanonicalName();
        throw new MockitoSerializationIssue(join(
                "The mock '" + mockName + "' of type '" + mockedType + "'",
                "The Java Standard Serialization reported an '" + ioe.getClass().getSimpleName() + "' saying :",
                "  " + ioe.getMessage()
        ), ioe);
    } finally {
        // unmark
        mockReplacementCompleted();
        mutex.unlock();
    }
}
 
开发者ID:mockito,项目名称:mockito-cglib,代码行数:69,代码来源:AcrossJVMSerializationFeature.java

示例4: writeReplace

import org.mockito.internal.util.MockUtil; //导入方法依赖的package包/类
/**
 * Custom implementation of the <code>writeReplace</code> method for serialization.
 *
 * Here's how it's working and why :
 * <ol>
 *     <li>
 *         <p>When first entering in this method, it's because some is serializing the mock, with some code like :
 * <pre class="code"><code class="java">
 *     objectOutputStream.writeObject(mock);
 * </code></pre>
 *         So, {@link ObjectOutputStream} will track the <code>writeReplace</code> method in the instance and
 *         execute it, which is wanted to replace the mock by another type that will encapsulate the actual mock.
 *         At this point, the code will return an {@link AcrossJVMMockSerializationProxy}.</p>
 *     </li>
 *     <li>
 *         <p>Now, in the constructor {@link AcrossJVMMockSerializationProxy#AcrossJVMMockSerializationProxy(Object)}
 *         the mock is being serialized in a custom way (using {@link MockitoMockObjectOutputStream}) to a
 *         byte array. So basically it means the code is performing double nested serialization of the passed
 *         <code>mockitoMock</code>.</p>
 *
 *         <p>However the <code>ObjectOutputStream</code> will still detect the custom
 *         <code>writeReplace</code> and execute it.
 *         <em>(For that matter disabling replacement via {@link ObjectOutputStream#enableReplaceObject(boolean)}
 *         doesn't disable the <code>writeReplace</code> call, but just just toggle replacement in the
 *         written stream, <strong><code>writeReplace</code> is always called by
 *         <code>ObjectOutputStream</code></strong>.)</em></p>
 *
 *         <p>In order to avoid this recursion, obviously leading to a {@link StackOverflowError}, this method is using
 *         a flag that marks the mock as already being replaced, and then shouldn't replace itself again.
 *         <strong>This flag is local to this class</strong>, which means the flag of this class unfortunately needs
 *         to be protected against concurrent access, hence the reentrant lock.</p>
 *     </li>
 * </ol>
 *
 *
 * @param mockitoMock The Mockito mock to be serialized.
 * @return A wrapper ({@link AcrossJVMMockSerializationProxy}) to be serialized by the calling ObjectOutputStream.
 * @throws ObjectStreamException
 */
public Object writeReplace(Object mockitoMock) throws ObjectStreamException {
    try {
        // reentrant lock for critical section. could it be improved ?
        mutex.lock();
        // mark started flag // per thread, not per instance
        // temporary loosy hack to avoid stackoverflow
        if(mockIsCurrentlyBeingReplaced()) {
            return mockitoMock;
        }
        mockReplacementStarted();

        return new AcrossJVMMockSerializationProxy(mockitoMock);
    } catch (IOException ioe) {
        MockUtil mockUtil = new MockUtil();
        MockName mockName = mockUtil.getMockName(mockitoMock);
        String mockedType = mockUtil.getMockSettings(mockitoMock).getTypeToMock().getCanonicalName();
        throw new MockitoSerializationIssue(join(
                "The mock '" + mockName + "' of type '" + mockedType + "'",
                "The Java Standard Serialization reported an '" + ioe.getClass().getSimpleName() + "' saying :",
                "  " + ioe.getMessage()
        ), ioe);
    } finally {
        // unmark
        mockReplacementCompleted();
        mutex.unlock();
    }
}
 
开发者ID:SpoonLabs,项目名称:astor,代码行数:67,代码来源:AcrossJVMSerializationFeature.java


注:本文中的org.mockito.internal.util.MockUtil.getMockName方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。