@Test public void should_set_serializable_mode_in_moc_settings() { //given final DoubleSearch doubles = findDoublesInClass(AnyTest.class); //when getConfiguration(doubles.findOneDefinition(AnyTest.withSerializationModeField)).createMockSettings(mockSettings); //then Mockito .verify(mockSettings) .serializable(SerializableMode.ACROSS_CLASSLOADERS); }
@Test public void should_resolve_serializable_mode_from_meta_annotations() { //given final DoubleSearch doubles = findDoublesInClass(AnyTest.class); //when getConfiguration(doubles.findOneDefinition(AnyTest.metaAnnotatedField)).createMockSettings(mockSettings); //then Mockito .verify(mockSettings) .serializable(SerializableMode.NONE); }
@Test public void should_create_mock_with_configuration() { //given final DoubleDefinition doubleDefinition = DoubleDefinition.builder() .name("mock") .doubleClass(Object.class) .doubleConfiguration(MockitoDoubleConfiguration.builder() .stubOnly(true) .verboseLogging(true) .serializableMode(SerializableMode.ACROSS_CLASSLOADERS) .answer(NoAnswer.class) .extraInterfaces(new Class[]{Closeable.class}) .invocationListeners(new Class[]{DoNothingInvocationListener.class}) .build()) .build(); //when doubleFactory.createMock(doubleDefinition); //then Mockito .verify(mockSettings) .stubOnly(); Mockito .verify(mockSettings) .verboseLogging(); Mockito .verify(mockSettings) .serializable(SerializableMode.ACROSS_CLASSLOADERS); Mockito .verify(mockSettings) .defaultAnswer(argThat(hasAnswerOfType(NoAnswer.class))); Mockito .verify(mockSettings) .extraInterfaces(argThat(new PresentInAnyOrder(AopInfrastructureBean.class, Closeable.class))); Mockito .verify(mockSettings) .invocationListeners(argThat(hasInstancesOf(DoNothingInvocationListener.class))); }
public byte[] call() throws Exception { AClassToBeMockedInThisTestOnlyAndInCallablesOnly mock = Mockito.mock( AClassToBeMockedInThisTestOnlyAndInCallablesOnly.class, Mockito.withSettings().serializable(SerializableMode.ACROSS_CLASSLOADERS) ); // use MethodProxy before mock.returningSomething(); return SimpleSerializationUtil.serializeMock(mock).toByteArray(); }
public DoubleSerializableModeSettings(SerializableMode serializableMode) { this.serializableMode = serializableMode; }
public boolean isSerializable() { return serializableMode != SerializableMode.NONE; }
public SerializableMode getSerializableMode() { return serializableMode; }
public MockSettings serializable() { return serializable(SerializableMode.BASIC); }
public MockSettings serializable(SerializableMode mode) { this.serializableMode = mode; return this; }
/** * 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); } }
/** * 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); } }
/** * Configures the mock to be serializable with a specific serializable mode. * With this feature you can use a mock in a place that requires dependencies to be serializable. * <p> * WARNING: This should be rarely used in unit testing. * <p> * The behaviour was implemented for a specific use case of a BDD spec that had an unreliable external dependency. This * was in a web environment and the objects from the external dependency were being serialized to pass between layers. * * <pre class="code"><code class="java"> * List serializableMock = mock(List.class, withSettings().serializable(SerializableMode.ACROSS_CLASSLOADERS)); * </code></pre> * * @param mode serialization mode * @return settings instance so that you can fluently specify other settings * @since 1.9.8 */ MockSettings serializable(SerializableMode mode);