com.google.gwtmockito
Class GwtMockito

java.lang.Object
  extended by com.google.gwtmockito.GwtMockito

public class GwtMockito
extends Object

A library to make Mockito-based testing of GWT applications easier. Most users won't have to reference this class directly and should instead use GwtMockitoTestRunner. Users who cannot use that class (e.g. tests using JUnit3) can invoke initMocks(java.lang.Object) directly in their setUp and tearDown() in their tearDown methods.

Note that calling initMocks(java.lang.Object) and tearDown() directly does not implement GwtMockitoTestRunner's behavior of implementing native methods and making final methods mockable. The only way to get this behavior is by using GwtMockitoTestRunner.

Once initMocks(java.lang.Object) has been invoked, test code can safely call GWT.create without exceptions. Doing so will return either a mock object registered with GwtMock, a fake object specified by a call to useProviderForType(java.lang.Class, com.google.gwtmockito.fakes.FakeProvider), or a new mock instance if no other binding exists. Fakes for types extending the following are provided by default:

The type returned from GWT.create will generally be the same as the type passed in. The exception is when GWT.create'ing a subclass of RemoteService - in this case, the result of GWT.create will be the Async version of that interface as defined by gwt-rpc.

If initMocks(java.lang.Object) is called manually, it is important to invoke tearDown() once the test has been completed. Failure to do so can cause state to leak between tests.

Author:
ekuefler@google.com (Erik Kuefler)
See Also:
GwtMockitoTestRunner, GwtMock

Constructor Summary
GwtMockito()
           
 
Method Summary
static void initMocks(Object owner)
          Causes all calls to GWT.create to be intercepted to return a mock or fake object, and populates any GwtMock-annotated fields with mockito mocks.
static void tearDown()
          Resets GWT.create to its default behavior.
static void useProviderForType(Class<?> type, FakeProvider<?> provider)
          Specifies that the given provider should be used to GWT.create instances of the given type and its subclasses.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GwtMockito

public GwtMockito()
Method Detail

initMocks

public static void initMocks(Object owner)
Causes all calls to GWT.create to be intercepted to return a mock or fake object, and populates any 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 MockitoAnnotations.initMocks(java.lang.Object), so there is no need to call that method separately. See the class description for more details.

Parameters:
owner - class to scan for GwtMock-annotated fields - almost always "this" in unit tests

tearDown

public static void tearDown()
Resets GWT.create to its default behavior. This method should be called after any test that called initMocks completes, usually in your test's tearDown method. Failure to do so can introduce unexpected ordering dependencies in tests.


useProviderForType

public static void useProviderForType(Class<?> type,
                                      FakeProvider<?> provider)
Specifies that the given provider should be used to GWT.create instances of the given type and its subclasses. Note that if you just want to return a Mockito mock from GWT.create, it's probably easier to use GwtMock instead.



Copyright © 2013. All Rights Reserved.