Skip to content

Instantly share code, notes, and snippets.

View mohak1712's full-sized avatar

Mohak Puri mohak1712

View GitHub Profile
@Component(modules = ApplicationModule.class)
@Singleton
public interface ApplicationComponent {
DataManager getDataManager();
}
public class MyTestingApp extends Application {
ApplicationComponent applicationComponent;
@Override
public void onCreate() {
super.onCreate();
}
public ApplicationComponent getComponent() {
public class UiTestApp extends MyTestingApp {
@Override
public ApplicationComponent getComponent() {
return DaggerApplicationComponent.builder()
.applicationModule(new TestApplicationModule(this))
.build();
}
}
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> activityRule =
new ActivityTestRule<>(MainActivity.class,false,false);
private MockWebServer webServer;
@Before
@Test
public void retryButton() {
webServer.setDispatcher(new MockServerDispatcher().new ErrorDispatcher());
activityRule.launchActivity(new Intent());
Espresso.onView(withId(R.id.btRetry)).perform(click());
Espresso.onView(withId(R.id.data)).check(matches(not(isDisplayed())));
Espresso.onView(withId(R.id.errorUI)).check(matches((isDisplayed())));
}
@Test
public void testHappyCondition() {
webServer.setDispatcher(new MockServerDispatcher().new RequestDispatcher());
activityRule.launchActivity(new Intent());
Espresso.onView(withId(R.id.progressBar2)).check(matches(not(isDisplayed())));
Espresso.onView(withId(R.id.button)).check(matches(not(isDisplayed())));
Espresso.onView(withId(R.id.textView)).check(matches(isDisplayed()));
Espresso.onView(withId(R.id.errorView)).check(matches(not(isDisplayed())));
class MockServerDispatcher {
/**
* Return ok response from mock server
*/
class RequestDispatcher extends Dispatcher {
@Override
public MockResponse dispatch(RecordedRequest request) {
if(request.getPath().equals("api/data")){
public class MockRunner extends AndroidJUnitRunner {
@Override
public void onCreate(Bundle arguments) {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
super.onCreate(arguments);
}
@Override
public Application newApplication(ClassLoader cl, String className, Context context)
@Singleton
public class DataManager {
private BaseApiManager mBaseApiManager;
@Inject
public DataManager(BaseApiManager baseApiManager) {
mBaseApiManager = baseApiManager;
}
@Module
public class ApplicationModule {
private Application mApplication;
public ApplicationModule(Application application) {
mApplication = application;
}
@Provides