@Override @SuppressWarnings("unchecked") // safe since we check whether the type is assignable public <T> T create(Class<?> createdType) { // If we're creating a RemoteService, assume that the result of GWT.create is being assigned // to the async version of that service. Otherwise, assume it's being assigned to the same // type we're creating. Class<?> assignedType = RemoteService.class.isAssignableFrom(createdType) ? getAsyncType((Class<? extends RemoteService>) createdType) : createdType; // First check if we have a GwtMock for this exact being assigned to and use it if so. if (registeredMocks.containsKey(assignedType)) { return (T) registeredMocks.get(assignedType); } // Next check if we have a fake provider that can provide a fake for the type being created. T fake = (T) getFakeFromProviderMap(createdType, registeredProviders); if (fake != null) { return fake; } // If nothing has been registered, just return a new mock for the type being assigned. return (T) mock(assignedType, new ReturnsCustomMocks()); }
/** * Causes all calls to GWT.create to be intercepted to return a mock or fake * object, and populates any {@link GwtMock}-annotated fields with mockito * mocks. This method should be usually be called during the setUp method of a * test case. Note that it explicitly calls * {@link MockitoAnnotations#initMocks}, so there is no need to call that * method separately. See the class description for more details. * * @param owner class to scan for {@link GwtMock}-annotated fields - almost * always "this" in unit tests */ public static void initMocks(Object owner) { // Create a new bridge and register built-in type providers bridge = new Bridge(); for (Entry<Class<?>, FakeProvider<?>> entry : DEFAULT_FAKE_PROVIDERS.entrySet()) { useProviderForType(entry.getKey(), entry.getValue()); } // Install the bridge and populate mock fields boolean success = false; try { setGwtBridge(bridge); registerGwtMocks(owner); MockitoAnnotations.initMocks(owner); success = true; } finally { if (!success) { tearDown(); } } }
/** * Causes all calls to GWT.create to be intercepted to return a mock or fake * object, and populates any {@link GwtMock}-annotated fields with mockito * mocks. This method should be usually be called during the setUp method of a * test case. Note that it explicitly calls * {@link MockitoAnnotations#initMocks}, so there is no need to call that * method separately. See the class description for more details. * * @param owner class to scan for {@link GwtMock}-annotated fields - almost * always "this" in unit tests */ public static void initMocks(Object owner) { // Create a new bridge and register built-in type providers bridge = new Bridge(); for (Entry<Class<?>, FakeProvider<?>> entry : DEFAULT_FAKE_PROVIDERS.entrySet()) { useProviderForType(entry.getKey(), entry.getValue()); } // Install the bridge and populate mock fields boolean success = false; try { setGwtBridge(bridge); registerGwtMocks(owner); MockitoAnnotations.initMocks(owner); success = true; } finally { if (!success) { tearDown(); } } }
@Override @SuppressWarnings("unchecked") // safe since we check whether the type is assignable public <T> T create(Class<?> createdType) { // If we're creating a RemoteService, assume that the result of GWT.create is being assigned // to the async version of that service. Otherwise, assume it's being assigned to the same // type we're creating. Class<?> assignedType = RemoteService.class.isAssignableFrom(createdType) ? getAsyncType((Class<? extends RemoteService>) createdType) : createdType; // First check if we have a GwtMock for this exact being assigned to and use it if so. if (registeredMocks.containsKey(assignedType)) { return (T) registeredMocks.get(assignedType); } // Next check if we have a fake provider that can provide a fake for the type being created. T fake = (T) getFakeFromProviderMap(createdType, registeredProviders); if (fake != null) { return fake; } // If nothing has been registered, just return a new mock for the type being assigned. return (T) mock(assignedType, new ReturnsCustomMocks()); }