Java 类org.mockito.mock.MockCreationSettings 实例源码

项目:spring-boot-concourse    文件:MockDefinitionTests.java   
@Test
public void createMock() throws Exception {
    MockDefinition definition = new MockDefinition("name", ExampleService.class,
            new Class<?>[] { ExampleExtraInterface.class },
            Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE, true);
    ExampleService mock = definition.createMock();
    MockCreationSettings<?> settings = new MockUtil().getMockSettings(mock);
    assertThat(mock).isInstanceOf(ExampleService.class);
    assertThat(mock).isInstanceOf(ExampleExtraInterface.class);
    assertThat(settings.getMockName().toString()).isEqualTo("name");
    assertThat(settings.getDefaultAnswer())
            .isEqualTo(Answers.RETURNS_SMART_NULLS.get());
    assertThat(settings.isSerializable()).isTrue();
    assertThat(MockReset.get(mock)).isEqualTo(MockReset.BEFORE);
}
项目:spring-boot-concourse    文件:SpyDefinitionTests.java   
@Test
public void createSpy() throws Exception {
    SpyDefinition definition = new SpyDefinition("name", RealExampleService.class,
            MockReset.BEFORE, true);
    RealExampleService spy = definition.createSpy(new RealExampleService("hello"));
    MockCreationSettings<?> settings = new MockUtil().getMockSettings(spy);
    assertThat(spy).isInstanceOf(ExampleService.class);
    assertThat(settings.getMockName().toString()).isEqualTo("name");
    assertThat(settings.getDefaultAnswer())
            .isEqualTo(Answers.CALLS_REAL_METHODS.get());
    assertThat(MockReset.get(spy)).isEqualTo(MockReset.BEFORE);
}
项目:astor    文件:MockHandlerFactory.java   
public InternalMockHandler create(MockCreationSettings settings) {
    InternalMockHandler handler = new MockHandlerImpl(settings);
    InternalMockHandler nullResultGuardian = new NullResultGuardian(handler);
    InternalMockHandler notifier = new InvocationNotifierHandler(nullResultGuardian, settings);

    return notifier;
}
项目:astor    文件:AcrossJVMSerializationFeature.java   
/**
 * Creates the wrapper that be used in the serialization stream.
 *
 * <p>Immediately serializes the Mockito mock using specifically crafted {@link MockitoMockObjectOutputStream},
 * in a byte array.</p>
 *
 * @param mockitoMock The Mockito mock to serialize.
 * @throws IOException
 */
public AcrossJVMMockSerializationProxy(Object mockitoMock) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new MockitoMockObjectOutputStream(out);

    objectOutputStream.writeObject(mockitoMock);

    objectOutputStream.close();
    out.close();

    MockCreationSettings mockSettings = new MockUtil().getMockSettings(mockitoMock);
    this.serializedMock = out.toByteArray();
    this.typeToMock = mockSettings.getTypeToMock();
    this.extraInterfaces = mockSettings.getExtraInterfaces();
}
项目:astor    文件:MockUtil.java   
public <T> T createMock(MockCreationSettings<T> settings) {
    MockHandler mockHandler = new MockHandlerFactory().create(settings);

    T mock = mockMaker.createMock(settings, mockHandler);

    Object spiedInstance = settings.getSpiedInstance();
    if (spiedInstance != null) {
        new LenientCopyTool().copyToMock(spiedInstance, mock);
    }

    return mock;
}
项目:astor    文件:MockUtil.java   
public <T> void resetMock(T mock) {
    InternalMockHandler oldHandler = (InternalMockHandler) getMockHandler(mock);
    MockCreationSettings settings = oldHandler.getMockSettings();
    MockHandler newHandler = new MockHandlerFactory().create(settings);

    mockMaker.resetMock(mock, newHandler, settings);
}
项目:astor    文件:MockHandlerFactoryTest.java   
@Test
//see issue 331
public void handle_result_must_not_be_null_for_primitives() throws Throwable {
    //given:
    MockCreationSettings settings = (MockCreationSettings) new MockSettingsImpl().defaultAnswer(new Returns(null));
    InternalMockHandler handler = new MockHandlerFactory().create(settings);

    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();

    //when:
    Object result = handler.handle(invocation);

    //then null value is not a valid result for a primitive
    assertNotNull(result);
    assertEquals(0, result);
}
项目:astor    文件:MockHandlerFactoryTest.java   
@Test
//see issue 331
public void valid_handle_result_is_permitted() throws Throwable {
    //given:
    MockCreationSettings settings = (MockCreationSettings) new MockSettingsImpl().defaultAnswer(new Returns(123));
    InternalMockHandler handler = new MockHandlerFactory().create(settings);

    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();

    //when:
    Object result = handler.handle(invocation);

    //then
    assertEquals(123, result);
}
项目:dexmaker    文件:InlineDexmakerMockMaker.java   
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
    if (adapter != null) {
        adapter.setHandler(newHandler);
    }
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:MockDefinitionTests.java   
@Test
public void createMock() throws Exception {
    MockDefinition definition = new MockDefinition("name", ExampleService.class,
            new Class<?>[] { ExampleExtraInterface.class },
            Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE, true);
    ExampleService mock = definition.createMock();
    MockCreationSettings<?> settings = new MockUtil().getMockSettings(mock);
    assertThat(mock).isInstanceOf(ExampleService.class);
    assertThat(mock).isInstanceOf(ExampleExtraInterface.class);
    assertThat(settings.getMockName().toString()).isEqualTo("name");
    assertThat(settings.getDefaultAnswer())
            .isEqualTo(Answers.RETURNS_SMART_NULLS.get());
    assertThat(settings.isSerializable()).isTrue();
    assertThat(MockReset.get(mock)).isEqualTo(MockReset.BEFORE);
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:SpyDefinitionTests.java   
@Test
public void createSpy() throws Exception {
    SpyDefinition definition = new SpyDefinition("name", RealExampleService.class,
            MockReset.BEFORE, true);
    RealExampleService spy = definition.createSpy(new RealExampleService("hello"));
    MockCreationSettings<?> settings = new MockUtil().getMockSettings(spy);
    assertThat(spy).isInstanceOf(ExampleService.class);
    assertThat(settings.getMockName().toString()).isEqualTo("name");
    assertThat(settings.getDefaultAnswer())
            .isEqualTo(Answers.CALLS_REAL_METHODS.get());
    assertThat(MockReset.get(spy)).isEqualTo(MockReset.BEFORE);
}
项目:fluent-bdd    文件:FluentMockito.java   
@SuppressWarnings("rawtypes") // The MockCreationListener interface forces this
@Override
public void onMockCreated(Object mock, MockCreationSettings settings) {
    mocks.add(mock);
}
项目:powermock    文件:PowerMockMaker.java   
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    return cglibMockMaker.createMock(settings, handler);
}
项目:powermock    文件:PowerMockMaker.java   
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    cglibMockMaker.resetMock(mock, newHandler, settings);
}
项目:powermock    文件:PowerMockMaker.java   
public MockCreationSettings getMockSettings() {
    final MockSettingsImpl mockSettings = new MockSettingsImpl();
    mockSettings.setMockName(new MockNameImpl(mock.getName()));
    mockSettings.setTypeToMock(mock);
    return mockSettings;
}
项目:powermock    文件:PowerMockMethodInterceptorFilter.java   
public PowerMockMethodInterceptorFilter(InternalMockHandler handler,
        MockCreationSettings mockSettings) {
    super(handler, mockSettings);
}
项目:mockito-cglib    文件:MethodInterceptorFilter.java   
public MethodInterceptorFilter(InternalMockHandler handler, MockCreationSettings mockSettings) {
    this.handler = handler;
    this.mockSettings = mockSettings;
}
项目:mockito-cglib    文件:CglibMockMaker.java   
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    InternalMockHandler mockitoHandler = cast(handler);
    new AcrossJVMSerializationFeature().enableSerializationAcrossJVM(settings);
    return new ClassImposterizer(new InstantiatorProvider().getInstantiator(settings)).imposterise(
            new MethodInterceptorFilter(mockitoHandler, settings), settings.getTypeToMock(), settings.getExtraInterfaces());
}
项目:mockito-cglib    文件:CglibMockMaker.java   
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    ((Factory) mock).setCallback(0, new MethodInterceptorFilter(cast(newHandler), settings));
}
项目:mockito-cglib    文件:AcrossJVMSerializationFeature.java   
/**
 * Creates the wrapper that be used in the serialization stream.
 *
 * <p>Immediately serializes the Mockito mock using specifically crafted
 * {@link AcrossJVMSerializationFeature.MockitoMockObjectOutputStream},
 * in a byte array.</p>
 *
 * @param mockitoMock The Mockito mock to serialize.
 * @throws IOException
 */
public AcrossJVMMockSerializationProxy(Object mockitoMock) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ObjectOutputStream objectOutputStream = new MockitoMockObjectOutputStream(out);

    objectOutputStream.writeObject(mockitoMock);

    objectOutputStream.close();
    out.close();

    MockCreationSettings mockSettings = new MockUtil().getMockSettings(mockitoMock);
    this.serializedMock = out.toByteArray();
    this.typeToMock = mockSettings.getTypeToMock();
    this.extraInterfaces = mockSettings.getExtraInterfaces();
}
项目:j2objc    文件:MockSettingsImpl.java   
public MockCreationSettings<T> confirm(Class<T> typeToMock) {
    return validatedSettings(typeToMock, this);
}
项目:j2objc    文件:IosMockMaker.java   
@Override
@SuppressWarnings("rawtypes")
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
  InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
  adapter.setHandler(newHandler);
}
项目:astor    文件:InvocationContainerImpl.java   
public InvocationContainerImpl(MockingProgress mockingProgress, MockCreationSettings mockSettings) {
    this.mockingProgress = mockingProgress;
    this.registeredInvocations = createRegisteredInvocations(mockSettings);
}
项目:astor    文件:InvocationContainerImpl.java   
private RegisteredInvocations createRegisteredInvocations(MockCreationSettings mockSettings) {
    return mockSettings.isStubOnly()
      ? new SingleRegisteredInvocation()
      : new DefaultRegisteredInvocations();
}
项目:astor    文件:InvocationNotifierHandler.java   
public InvocationNotifierHandler(InternalMockHandler<T> mockHandler, MockCreationSettings settings) {
    this.mockHandler = mockHandler;
    this.invocationListeners = settings.getInvocationListeners();
}
项目:astor    文件:InvocationNotifierHandler.java   
public MockCreationSettings getMockSettings() {
    return mockHandler.getMockSettings();
}
项目:astor    文件:NullResultGuardian.java   
public MockCreationSettings getMockSettings() {
    return delegate.getMockSettings();
}
项目:astor    文件:MockHandlerImpl.java   
public MockHandlerImpl(MockCreationSettings mockSettings) {
    this.mockSettings = mockSettings;
    this.mockingProgress = new ThreadSafeMockingProgress();
    this.matchersBinder = new MatchersBinder();
    this.invocationContainerImpl = new InvocationContainerImpl(mockingProgress, mockSettings);
}
项目:astor    文件:MockHandlerImpl.java   
public MockCreationSettings getMockSettings() {
    return mockSettings;
}
项目:astor    文件:MethodInterceptorFilter.java   
public MethodInterceptorFilter(InternalMockHandler handler, MockCreationSettings mockSettings) {
    this.handler = handler;
    this.mockSettings = mockSettings;
}
项目:astor    文件:CglibMockMaker.java   
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
    InternalMockHandler mockitoHandler = cast(handler);
    new AcrossJVMSerializationFeature().enableSerializationAcrossJVM(settings);
    return ClassImposterizer.INSTANCE.imposterise(
            new MethodInterceptorFilter(mockitoHandler, settings), settings.getTypeToMock(), settings.getExtraInterfaces());
}
项目:astor    文件:CglibMockMaker.java   
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    ((Factory) mock).setCallback(0, new MethodInterceptorFilter(cast(newHandler), settings));
}
项目:astor    文件:MockSettingsImpl.java   
public MockCreationSettings<T> confirm(Class<T> typeToMock) {
    return validatedSettings(typeToMock, this);
}
项目:astor    文件:MockUtil.java   
public MockCreationSettings getMockSettings(Object mock) {
    return getMockHandler(mock).getMockSettings();
}
项目:dexmaker    文件:DexmakerMockMaker.java   
@Override
public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings) {
    InvocationHandlerAdapter adapter = getInvocationHandlerAdapter(mock);
    adapter.setHandler(newHandler);
}
项目:mockito-cglib    文件:AcrossJVMSerializationFeature.java   
/**
 * Enable serialization serialization that will work across classloaders / and JVM.
 * <p/>
 * <p>Only enable if settings says the mock should be serializable. In this case add the
 * {@link AcrossJVMMockitoMockSerializable} to the extra interface list.</p>
 *
 * @param settings Mock creation settings.
 * @param <T>      Type param to not be bothered by the generics
 */
public <T> void enableSerializationAcrossJVM(MockCreationSettings<T> settings) {
    if (settings.getSerializableMode() == SerializableMode.ACROSS_CLASSLOADERS) {
        // havin faith that this set is modifiable
        // TODO use a proper way to add the interface
        settings.getExtraInterfaces().add(AcrossJVMMockitoMockSerializable.class);
    }
}
项目:astor    文件:AcrossJVMSerializationFeature.java   
/**
 * Enable serialization serialization that will work across classloaders / and JVM.
 *
 * <p>Only enable if settings says the mock should be serializable. In this case add the
 * {@link AcrossJVMMockitoMockSerializable} to the extra interface list.</p>
 *
 * @param settings Mock creation settings.
 * @param <T> Type param to not be bothered by the generics
 */
public <T> void enableSerializationAcrossJVM(MockCreationSettings<T> settings) {
    if (settings.getSerializableMode() == SerializableMode.ACROSS_CLASSLOADERS) {
        // havin faith that this set is modifiable
        // TODO use a proper way to add the interface
        settings.getExtraInterfaces().add(AcrossJVMMockitoMockSerializable.class);
    }
}
项目:astor    文件:MockMaker.java   
/**
 * If you want to provide your own implementation of {@code MockMaker} this method should:
 * <ul>
 *     <li>Create a proxy object that implements {@code settings.typeToMock} and potentially also {@code settings.extraInterfaces}.</li>
 *     <li>You may use the information from {@code settings} to create/configure your proxy object.</li>
 *     <li>Your proxy object should carry the {@code handler} with it. For example, if you generate byte code
 *     to create the proxy you could generate an extra field to keep the {@code handler} with the generated object.
 *     Your implementation of {@code MockMaker} is required to provide this instance of {@code handler} when
 *     {@link #getHandler(Object)} is called.
 *     </li>
 * </ul>
 *
 * @param settings - mock creation settings like type to mock, extra interfaces and so on.
 * @param handler See {@link org.mockito.invocation.MockHandler}.
 *                <b>Do not</b> provide your own implementation at this time. Make sure your implementation of
 *                {@link #getHandler(Object)} will return this instance.
 * @param <T> Type of the mock to return, actually the <code>settings.getTypeToMock</code>.
 * @return The mock instance.
 * @since 1.9.5
 */
<T> T createMock(
        MockCreationSettings<T> settings,
        MockHandler handler
);
项目:astor    文件:MockMaker.java   
/**
 * Replaces the existing handler on {@code mock} with {@code newHandler}.
 *
 * <p>The invocation handler actually store invocations to achieve
 * stubbing and verification. In order to reset the mock, we pass
 * a new instance of the invocation handler.</p>
 *
 * <p>Your implementation should make sure the {@code newHandler} is correctly associated to passed {@code mock}</p>
 *
 * @param mock The mock instance whose invocation handler is to be replaced.
 * @param newHandler The new invocation handler instance.
 * @param settings The mock settings - should you need to access some of the mock creation details.
 * @since 1.9.5
 */
void resetMock(
        Object mock,
        MockHandler newHandler,
        MockCreationSettings settings
);
项目:astor    文件:InternalMockHandler.java   
MockCreationSettings getMockSettings();