Refine search
/** * Creates mock object of given class or interface. * <p> * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @return mock object */ public static <T> T mock(Class<T> classToMock) { return mock(classToMock, withSettings().defaultAnswer(RETURNS_DEFAULTS)); }
/** * Creates mock object of given class or interface. * <p> * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @return mock object */ @CheckReturnValue public static <T> T mock(Class<T> classToMock) { return mock(classToMock, withSettings()); }
private MockSettings withSettingsUsing(GenericMetadataSupport returnTypeGenericMetadata, MockCreationSettings parentMockSettings) { MockSettings mockSettings = returnTypeGenericMetadata.hasRawExtraInterfaces() ? withSettings().extraInterfaces(returnTypeGenericMetadata.rawExtraInterfaces()) : withSettings(); return propagateSerializationSettings(mockSettings, parentMockSettings) .defaultAnswer(returnsDeepStubsAnswerUsing(returnTypeGenericMetadata)); }
@Test public void proxyingWorksIfInfoReturnsNullEntityManagerInterface() { EntityManagerFactory emf = mock(EntityManagerFactory.class, withSettings().extraInterfaces(EntityManagerFactoryInfo.class)); // EntityManagerFactoryInfo.getEntityManagerInterface returns null assertThat(SharedEntityManagerCreator.createSharedEntityManager(emf), is(notNullValue())); }
/** * Creates mock with a specified strategy for its answers to interactions. * It's quite advanced feature and typically you don't need it to write decent tests. * However it can be helpful when working with legacy systems. * <p> * It is the default answer so it will be used <b>only when you don't</b> stub the method call. * * <pre class="code"><code class="java"> * Foo mock = mock(Foo.class, RETURNS_SMART_NULLS); * Foo mockTwo = mock(Foo.class, new YourOwnAnswer()); * </code></pre> * * <p>See examples in javadoc for {@link Mockito} class</p> * * @param classToMock class or interface to mock * @param defaultAnswer default answer for unstubbed methods * * @return mock object */ public static <T> T mock(Class<T> classToMock, Answer defaultAnswer) { return mock(classToMock, withSettings().defaultAnswer(defaultAnswer)); }
/** * Creates class mock with a specified strategy for its answers to * interactions. It's quite advanced feature and typically you don't need it * to write decent tests. However it can be helpful when working with legacy * systems. * <p> * It is the default answer so it will be used <b>only when you don't</b> * stub the method call. * * <pre> * mockStatic(Foo.class, RETURNS_SMART_NULLS); * mockStatic(Foo.class, new YourOwnAnswer()); * </pre> * * @param classMock * class to mock * @param defaultAnswer * default answer for unstubbed methods */ public static void mockStatic(Class<?> classMock, @SuppressWarnings("rawtypes") Answer defaultAnswer) { mockStatic(classMock, withSettings().defaultAnswer(defaultAnswer)); }
private static ExecutionGraphManipulator getMockedGraph(int address, HeapItem value) { ExecutionGraphManipulator manipulator = mock(ExecutionGraphManipulator.class); BuilderInstruction instruction = mock(BuilderInstruction.class, withSettings().extraInterfaces(OneRegisterInstruction.class)); when(((OneRegisterInstruction) instruction).getRegisterA()).thenReturn(REGISTER); when(manipulator.getRegisterConsensus(address, REGISTER)).thenReturn(value); when(manipulator.getInstruction(address)).thenReturn(instruction); return manipulator; }
private static Object spyInstance(Field field, Object instance) { return Mockito.mock(instance.getClass(), withSettings().spiedInstance(instance) .defaultAnswer(CALLS_REAL_METHODS) .name(field.getName())); }
/** * Creates mock with a specified strategy for its answers to interactions. * It's quite advanced feature and typically you don't need it to write * decent tests. However it can be helpful when working with legacy systems. * <p> * It is the default answer so it will be used <b>only when you don't</b> * stub the method call. * * <pre> * Foo mock = mock(Foo.class, RETURNS_SMART_NULLS); * Foo mockTwo = mock(Foo.class, new YourOwnAnswer()); * </pre> * * <p> * See examples in javadoc for {@link Mockito} class * </p> * * @param classToMock * class or interface to mock * @param defaultAnswer * default answer for unstubbed methods * * @return mock object */ public static <T> T mock(Class<T> classToMock, @SuppressWarnings("rawtypes") Answer defaultAnswer) { return mock(classToMock, withSettings().defaultAnswer(defaultAnswer)); }
private static JobEntryInterface createJobEntry( String directory ) { JobEntryInterface jobEntryInterface = mock( JobEntryInterface.class, withSettings().extraInterfaces( HasRepositoryDirectories.class ) ); when( jobEntryInterface.isReferencedObjectEnabled() ).thenReturn( new boolean[] { true } ); doAnswer( invocationOnMock -> new ObjectLocationSpecificationMethod[] { ObjectLocationSpecificationMethod.REPOSITORY_BY_NAME } ) .when( ( (HasRepositoryDirectories) jobEntryInterface ) ).getSpecificationMethods(); doAnswer( invocationOnMock -> new String[] { directory } ) .when( (HasRepositoryDirectories) jobEntryInterface ).getDirectories(); return jobEntryInterface; }
/** * Creates mock with a specified strategy for its answers to interactions. * It's quite an advanced feature and typically you don't need it to write decent tests. * However it can be helpful when working with legacy systems. * <p> * It is the default answer so it will be used <b>only when you don't</b> stub the method call. * * <pre class="code"><code class="java"> * Foo mock = mock(Foo.class, RETURNS_SMART_NULLS); * Foo mockTwo = mock(Foo.class, new YourOwnAnswer()); * </code></pre> * * <p>See examples in javadoc for {@link Mockito} class</p> * * @param classToMock class or interface to mock * @param defaultAnswer default answer for unstubbed methods * * @return mock object */ @CheckReturnValue public static <T> T mock(Class<T> classToMock, Answer defaultAnswer) { return mock(classToMock, withSettings().defaultAnswer(defaultAnswer)); }
return MOCKITO_CORE.mock((Class<T>) object.getClass(), withSettings() .spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));
private static StepMetaInterface createStepMeta( String directory ) { StepMetaInterface stepMetaInterface = mock( StepMetaInterface.class, withSettings().extraInterfaces( HasRepositoryDirectories.class ) ); when( stepMetaInterface.isReferencedObjectEnabled() ).thenReturn( new boolean[] { true } ); doAnswer( invocationOnMock -> new ObjectLocationSpecificationMethod[] { ObjectLocationSpecificationMethod.REPOSITORY_BY_NAME } ) .when( ( (HasRepositoryDirectories) stepMetaInterface ) ).getSpecificationMethods(); doAnswer( invocationOnMock -> new String[] { directory } ) .when( (HasRepositoryDirectories) stepMetaInterface ).getDirectories(); return stepMetaInterface; }
/** * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors. * <p> * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks. * <p> * <b>If you use <code>@Mock</code> annotation then you've got naming mocks for free!</b> <code>@Mock</code> uses field name as mock name. {@link Mock Read more.} * <p> * * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param name of the mock * @return mock object */ public static <T> T mock(Class<T> classToMock, String name) { return mock(classToMock, withSettings() .name(name) .defaultAnswer(RETURNS_DEFAULTS)); }
return MOCKITO_CORE.mock((Class<T>) object.getClass(), withSettings() .spiedInstance(object) .defaultAnswer(CALLS_REAL_METHODS));
private BuilderInstruction buildInstruction22t(Opcode opcode, int offset) { BuilderInstruction instruction = mock(BuilderInstruction.class, withSettings().extraInterfaces(Instruction22t.class)); when(location.getInstruction()).thenReturn(instruction); when(instruction.getLocation()).thenReturn(location); when(instruction.getCodeUnits()).thenReturn(0); when(instruction.getOpcode()).thenReturn(opcode); when(((Instruction22t) instruction).getRegisterA()).thenReturn(ARG1_REGISTER); when(((Instruction22t) instruction).getRegisterB()).thenReturn(ARG2_REGISTER); when(((Instruction22t) instruction).getCodeOffset()).thenReturn(offset); return instruction; }
/** * Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors. * <p> * Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. * <b>If you have too many mocks then refactor the code</b> so that it's easy to test/debug without necessity of naming mocks. * <p> * <b>If you use <code>@Mock</code> annotation then you've got naming mocks for free!</b> <code>@Mock</code> uses field name as mock name. {@link Mock Read more.} * <p> * * See examples in javadoc for {@link Mockito} class * * @param classToMock class or interface to mock * @param name of the mock * @return mock object */ @CheckReturnValue public static <T> T mock(Class<T> classToMock, String name) { return mock(classToMock, withSettings() .name(name) .defaultAnswer(RETURNS_DEFAULTS)); }
@CheckReturnValue public static <T> T spy(Class<T> classToSpy) { return MOCKITO_CORE.mock(classToSpy, withSettings() .useConstructor() .defaultAnswer(CALLS_REAL_METHODS));
@Test public void handlerMappingOrder() { HandlerMapping hm1 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class)); HandlerMapping hm2 = mock(HandlerMapping.class, withSettings().extraInterfaces(Ordered.class)); when(((Ordered) hm1).getOrder()).thenReturn(1); when(((Ordered) hm2).getOrder()).thenReturn(2); when((hm1).getHandler(any())).thenReturn(Mono.just((Supplier<String>) () -> "1")); when((hm2).getHandler(any())).thenReturn(Mono.just((Supplier<String>) () -> "2")); StaticApplicationContext context = new StaticApplicationContext(); context.registerBean("b2", HandlerMapping.class, () -> hm2); context.registerBean("b1", HandlerMapping.class, () -> hm1); context.registerBean(HandlerAdapter.class, SupplierHandlerAdapter::new); context.registerBean(HandlerResultHandler.class, StringHandlerResultHandler::new); context.refresh(); DispatcherHandler dispatcherHandler = new DispatcherHandler(context); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); dispatcherHandler.handle(exchange).block(Duration.ofSeconds(0)); assertEquals("1", exchange.getResponse().getBodyAsString().block(Duration.ofSeconds(5))); }
return mock(classToMock, withSettings().defaultAnswer(new AnswerReturnValuesAdapter(returnValues)));