Upgrade to Pro — share decks privately, control downloads, hide ads and more …

dagger2

funnelbit
April 20, 2015
520

 dagger2

funnelbit

April 20, 2015
Tweet

Transcript

  1. ௨৴෦෼ public class BkumaFormalServiceProvider implements BkumaServiceProvider {
 …
 @Override
 public

    BkumaService getService() {
 OkHttpClient client = new OkHttpClient();
 
 RestAdapter restAdapter = new RestAdapter.Builder()
 .setClient(new OkClient(client))
 .setEndpoint(BkumaService.END_POINT)
 .build();
 return restAdapter.create(BkumaService.class);
 } …
  2. .PEVMF͔Β*OKFDU @Module
 public class BkumaModule {
 private final BkumaService mBkumaService;


    public BkumaModule(BkumaGirlsApplication application) {
 mBkumaService = new BkumaFormalServiceProvider(application .getApplicationContext()).getService();
 
 @Provides @Singleton
 EntryManager provideEntryManager() {
 return new EntryManager(mBkumaService);
 } … public class MainActivity extends ActionBarActivity { @Inject EntryManager mEntryManager; @Override
 protected void onCreate(Bundle savedInstanceState) {
 … ((BkumaGirlsApplication)getApplication()).component() .inject(this); } … ௨৴෦෼
  3. $PNQPOFOUΛ࡞Δ public class BKUMAGirlsApplication extends Application {
 @Singleton
 @Component(modules =

    BkumaModule.class)
 public interface ApplicationComponent {
 void inject(MainActivity mainActivity);
 }
 
 private ApplicationComponent mComponent;
 
 @Override
 public void onCreate() {
 mComponent = DaggerBKUMAGirlsApplication_ApplicationComponent.builder()
 .bkumaModule(new BkumaModule(this))
 .build();
 }
 
 public ApplicationComponent component() {
 return mComponent;
 }
 }
  4. .PEVMF ςετ @Module
 public class BkumaTestModule {
 private final BkumaService

    mBkumaService; …
 public BkumaTestModule(InstrumentationTestCase instrumentationTestCase) { mBkumaService = new BkumaMockServiceProvider(mInstrumentationTestCase .getInstrumentation().getContext()).getService();
 }
 
 @Provides
 EntryManager provideEntryManager() {
 return new EntryManager(mBkumaService);
 } … 5FTU༻ #LVNB4FSWJDF
  5. ௨৴෦෼ʢςετʣ @Override
 public BkumaService getService() {
 RestAdapter restAdapter = new

    RestAdapter
 .Builder()
 .setEndpoint(BkumaService.END_POINT)
 .build();
 
 MockRestAdapter mockRestAdapter = MockRestAdapter.from(restAdapter);
 mockRestAdapter.setDelay(0); …(ଓ͖·͢) 5FTU༻.PDL
  6. ௨৴෦෼ʢςετʣ …
 BkumaService bkumaService = new BkumaService() {
 @Override
 public

    void getTopEntries(@Query("limit") int limit, Callback<List<EntryResponse>> callback) {
 callback.success( TestUtils.createDummyEntry(mContext, limit),
 createDummyJsonResponse()
 );
 } }); return mockRestAdapter.create( BkumaService.class, bkumaService ); %VNNZ 3FTQPOTF
  7. *OKFDU ςετ public class BkumaEntryManagerTest extends InstrumentationTestCase {
 …
 @Inject


    EntryManager mEntryManager;
 … public testGetEntries() { assertEquals( "ΤϯτϦऔಘͰ͖͍ͯΔ", false, mEntryManager.getEntries().isEmpty()); }
 }