Skip to content

Instantly share code, notes, and snippets.

@doridori
Last active May 14, 2022 10:01
Show Gist options
  • Save doridori/602f00e37fdeeac7756d to your computer and use it in GitHub Desktop.
Save doridori/602f00e37fdeeac7756d to your computer and use it in GitHub Desktop.
Testing with Dagger, Retrofit & MockWebServer gists
compile 'com.squareup.retrofit:retrofit:1.6.1'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.squareup.okhttp:mockwebserver:2.0.0'
compile 'com.squareup.dagger:dagger:1.2.+'
provided 'com.squareup.dagger:dagger-compiler:1.2.+'
androidTestCompile 'org.mockito:mockito-core:1.9.5'
androidTestCompile 'com.google.dexmaker:dexmaker:1.1'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.1'
/**
* Allows easy swapping of production and test modules to satisfy Dagger dependencies
*/
public class DaggerHelper
{
//DAGGER
private static ObjectGraph sObjectGraph;
private static final List<Object> productionModules;
static
{
productionModules = new ArrayList<Object>();
productionModules.add(new ServerApiModule());
}
/**
* Init the dagger object graph with production modules
*/
public synchronized static void initProductionModules()
{
initWithModules(productionModules);
}
/**
* If passing in test modules make sure to override = true in the @Module annotation
*/
public synchronized static void initWithTestModules(Object... testModules)
{
initWithModules(getModulesAsList(testModules));
}
/**
* Will create a new object graph and therefore reset any previous modules set
*
* @param modules
*/
private synchronized static void initWithModules(List<Object> modules)
{
sObjectGraph = ObjectGraph.create(modules.toArray());
}
private synchronized static List<Object> getModulesAsList(Object... extraModules)
{
List<Object> allModules = new ArrayList<Object>();
allModules.addAll(productionModules);
allModules.addAll(Arrays.asList(extraModules));
return allModules;
}
/**
* Dagger convience method - will inject the fields of the passed in object
*/
public synchronized static void inject(Object object)
{
sObjectGraph.inject(object);
}
}
@Override
protected void tearDown() throws Exception
{
super.tearDown();
DaggerHelper.initProductionModules(); //make sure any mock web server module overrides are removed
}
/**
* Allows a MockWebServer instance to be scripted for the api service
*/
@Module(
overrides = true,
library = true
)
public class TestServerApiModule
{
private final MockWebServer mMockWebServer;
public TestServerApiModule()
{
mMockWebServer = new MockWebServer();
mMockWebServer.play();
}
@Provides
MockWebServer providerMockWebServer()
{
return mMockWebServer;
}
@Provides
ServerApi providerServerApi()
{
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(mMockWebServer.getUrl("/").toString())
.build();
ServerApi api = restAdapter.create(ServerApi.class);
return api;
}
}
public void testSomething()
{
TestServerApiModule testServerApiModule = new TestServerApiModule();
DaggerHelper.initWithTestModules(testServerApiModule);
//script MockWebServer
MockResponse mockResponse = new MockResponse().setResponseCode(500);
testServerApiModule.getMockWebServer().enqueue(mockResponse);
//test something
new SomeClassThatUsesTheApi().doSomething();
...
<assert something>
}
@Module(
injects = Example.class
)
public class ServerApiModule
{
@Provides
ServerApi providerServerApi()
{
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(ServerConst.API_ENDPOINT)
.build();
ServerApi api = restAdapter.create(ServerApi.class);
return api;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment