Explain Dagger test for android application

 Some of the advantages of using dependency injection frameworks such as Dagger is that it enables

testing of the code. This document explores some strategies built with Dagger to test android
application. You need to replace functional integration for testing your android applications

To more information visit:android course
Replace functional integration or end-to- end test bindings in android application.
Functional or integration or end-to - end tests typically use the manufacturing android application, but
replace fakes. You should not use mocks in large functional tests for persistence, backend, and auth
systems, leaving the rest of the android application to work normally. Moreover, this approach lends
itself to providing one (or maybe a limited number of finite) test configurations, where the test
configuration replaces some of the prod configuration bindings. There are several options to test
android application. They are as follows.
Option 1:
Override module sub classification bindings in android application
The easiest way to remove bindings in a test portion is to override the @Provides methods in a subclass
of the modules. (But see Problems below.)
When you build an instance of your Dagger component you transfer the modules it uses in instances.
(You do not have to pass instances for modules with no-arg constructors or those without instance
methods, but you can.) That means that you can pass instances of subclasses of those modules, and
those subclasses can override some @Provides methods to replace some bindings.
@Component(modules = {AuthModul.class, or * ... * or})
MyAndroid applicationComponent interface {or * ... *}
@The Modul
AuthModule classe
@Provides authManager(AuthManagerImpl)
Return Implant;
}
}

The FakeAuthModul class extends AuthModule
@Überride
authManager(AuthManagerImpl impl)
Returns FakeAuthManager) (new;
}
}
TestingComponent = DaggerMyAndroid applicationComponent.builder)
authModule(new ModuleFakeAuth))
The.build);
But this method does have its limitations.
Using a subclass module can't change the binding graph's static shape: it can't add or delete bindings, or
change dependency on bindings. In particular, to:
Overriding a @Provides method cannot change its types of parameters, and narrowing the type of
return does not affect the binding graph as Dagger understands. In the above case, testing Components
still needs a binding for AuthManagerImpl and all of its dependencies, although not used.
Likewise, the overriding module cannot add bindings to the graph including new multi binding inputs
(although a SET VALUES function can still override to return a different set). All new @Provides methods
are secretly ignored by Dagger within the subclass. Practically this means your fakes are unable to take
advantage of the injection of dependency.
@Provides methods that are overriding in this way can not be static, so they can not elide their module
instances.
Option 2:
Separate configurations for the modules in android application
The approach requires that the modules in the framework be built more upfront. Each device design
(production and testing) uses a different configuration for the components. The type of test component
extends the type of production component, and installs a different set of modules.
@Component(s) =
OAuthModule.class, or Auth true
FooServiceModule.class, or Backend Real

OtherClassAndroid applicationModule,
or * ... * and})
ProductionComponent interface
Windows server);
}
@Component(s) =
FakeAuthModule.class, or AutoFake
FakeFooServiceModule.class, or Backend fake
OtherClassAndroid applicationModule,
or * ... *)
TestComponent interface extends the ProductionComponent to
FakeAuthManager forgery);
FakeFooService forgery);
}
DaggerTestComponent.builder()
Notice that the interface of the test part should attach provision handles to the fake instances
(fakeAuthManager) (and fakeFooService)) (so that if necessary the test should access them to monitor
the harness.
Organize Research Modules in android application
Module classes are a kind of utility class. IT is a set of independent @Provides methods, each of which
can be used by the injector to provide the framework with some type used.
@Providers

Although several @Provides methods may be related in that one relies on the form provided by another,
they usually do not call each other directly or rely on the same mutable state. Several @Provides
methods refer to the same instance field, in which case they are not actually independent. The advice
given here treats @Provides methods as utility methods anyway because it leads to modules that are
not independent of each other.

So how do you determine which methods @Provides will go together into one class of module. One way
to think about this is to classify bindings into published bindings and internal bindings, and then further
decide which of the published bindings has reasonable alternatives.
Published bindings are those which provide features that are used by other parts of the android
application. Types such as AuthManager or User or DocDatabase are published: They are bound in a
module so that they can be used by the rest of the android application.
Bindings in android application
The rest are internal bindings. The bindings which are used in implementing some published form and
which are not intended to be used except as part of it. For example, the OAuth client ID or
OAuthKeyStore configuration bindings are intended to be used only by AuthManager's OAuth
implementation, and not by the rest of the android application. Usually these bindings are for package-
private types or are qualified by private package- qualifiers.
Some published bindings, particularly for testing, will have reasonable alternatives and others won't. For
example, alternative bindings are likely to exist for a type such as AuthManager: one for testing, others
for different authentication or authorization protocols.
But on the other hand, if the AuthManager interface has a method that returns the user currently
logged in, you might want to publish a binding that provides the user by simply calling on the
AuthManager to getCurrentUser. The published binding is unlikely any alternative will ever be required.
Once you have categorized your bindings with reasonable alternatives into published bindings,
published bindings without reasonable alternatives and internal bindings, consider arranging them into
modules like this.
One module, with a reasonable alternative for each published binding. (If you write alternatives as well,
each one has its own module.) The module contains exactly one published binding, as well as all the
internal bindings provided by the published binding.
All bindings written without reasonable alternatives go into modules grouped along functional lines.
Each of the published-binding modules should include the non-reasonable-alternative modules which
require each of the public bindings.
Reported bindings
Documenting every module by explaining reported bindings that it provides is a good idea.
Here is an example which uses the auth domain. If an AuthManager interface exists, it may have an
OAuth implementation and a fake check implementation. As above, the current user could have an
obvious binding that you wouldn't expect to change between configurations.
or * *

Provides auth bindings that do not change the various auth configurations.
As in the current user.
* or
@The Modul
AuthModule classe
@Provides static User CurrentUser(AuthManager)
AuthManager.currentUser) (returns;
}
}
or * * Provides OAuth-using {@link AuthManager}. * or
@Module(includes = AuthModule.class) or Includes bindings with no alternative.
OAutModule class
@Enables static authManager(OAuthManager authManager)
Switch authManager back;
}
Other bindings which OAuthManager only uses.
}
or * * Provides for checking a false {@link AuthManager}. * or
@Module(includes = AuthModule.class) or Contains bindings with no alternative.
Class ModulFakeAuth
@Enables static authManager(FakeAuthManager authManager)
Switch authManager back;
}
Other only use FakeAuthManager bindings.
}

The real modules will then be used in your production setup, and the fake modules will be configured
for testing, as mentioned above.
You don't need to use Dagger for unit tests of one class
You don't need to use Dagger to instantiate the class if you want to write a small unit test that tests just
one @Inject-annotated object. If you want to write a typical unit test, you can call the @Inject-
annotated constructor and methods directly and set the @Inject-annotated fields directly, if any, to pass
fake or mock dependencies just like you would if they were not annotated.
ThingDoer ending class
Private final receiver ThingGetter;
Private end putter ThingPutter;
@Inject ThingDoer(ThingGetter getter, putter in ThingPutter)
This.getter = receiver;
Such.putter = putter;
}
TheThing(in howManyTimes) string {or * ... * or}
}
ThingDoerTest, public class
@Check @ Check
TestDoTheThing() public void
ThingDoer doer = new ThingDoer(fakeGetter);
AssertEquals('done 'and doer.doThing(5));
}
}
Conclusion
I hope you reach to a conclusion about the dagger test in the android application. You can learn more
through android online training.

Comments

Popular posts from this blog

Android App Project Package Structure (Android Studio)

ImageView and ImageButton in Android

Developing Android unit and instrumentation tests - Tutorial