This Google Testing Blog post lists some strategies for making code testable. One item says in part:
Ask for things, Don't look for things (aka Dependency Injection / Law of Demeter): OK, you got rid of your new operators in you application code. But how do I get a hold of the dependencies. Simple: Just ask for all of the collaborators you need in your constructor.
In other words, do this:
Foo(final Bar bar) {
mBar = bar;
}
Not this:
Foo() {
mBar = Bar.getBar(); // or new Bar();
}
The reason for this is obvious: it lets you test Foo by passing it a mock Bar. Since Android components require no-arg constructors, the equivalent is to pass their arguments via an extras Bundle.
How do you apply this principle in Android when the things the component needs are not Parcelable or Serializable?
Aucun commentaire:
Enregistrer un commentaire