<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Androidsx</title>
	<atom:link href="http://www.androidsx.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.androidsx.com</link>
	<description>Our Android apps and much more!</description>
	<lastBuildDate>Fri, 05 Mar 2010 12:55:53 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to make money with Android &#8211; business models</title>
		<link>http://www.androidsx.com/how-to-make-money-with-android-business-models/</link>
		<comments>http://www.androidsx.com/how-to-make-money-with-android-business-models/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 12:25:15 +0000</pubDate>
		<dc:creator>Omar Pera Mira</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[android market]]></category>

		<guid isPermaLink="false">http://www.androidsx.com/?p=162</guid>
		<description><![CDATA[Mark Murphy has been writing really interesting posts in Android guys about the different business models around Android platform. We have chosen the most relevant ones adding some additional information:

Build the app and sell it via the Android Market
Give a free app, supported by ads (Adsense mobile, Admob, etc.)
Give a free app, sell the customizations: [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Mark Murphy" href="http://commonsware.com/mmurphy" target="_blank">Mark Murphy</a> has been writing really interesting posts in <a href="http://www.androidguys.com/ ">Android guys</a> about the different business models around Android platform. We have chosen the most relevant ones adding some additional information:</p>
<ul>
<li>Build the app and <strong>sell</strong> it via the Android Market</li>
<li>Give a free app, <strong>supported by ads</strong> (<a href="https://www.google.com/adsense/www/en_US/mobile/">Adsense mobile</a>, <a href="http://www.admob.com">Admob</a>, etc.)</li>
<li>Give a free app, <strong>sell the customizations</strong>: plugins, analytics, additional features, account (e.g. <a href="http://www.rememberthemilk.com/services/android/">RTM</a>, <a href="http://www.spotify.com/es/mobile/android/">Spotify</a>), etc.</li>
<li>Sell <strong>development tools</strong>: GUI toolkits, portability tool, &#8230;</li>
<li>Develop <strong>custom applications</strong> for others (e.g. <a href="http://droiders.com/">Droiders</a>)</li>
<li>Became a <strong>trainer</strong> of Android development</li>
<li>Specialize in <strong>porting iPhone, Windows Mobile, J2ME applications</strong> to Android.</li>
<li>Promote your ability to port some critical parts of the apps to the <strong>Android Native Development Kit</strong> (NDK)</li>
<li>Became an <strong>specialist building cross-platform apps</strong> using HTML, Javascript and CSS (e.g. PhoneGap, Titanium, Rhodes)</li>
<li>Became an expert to build apps with GPS and backend capabilities of <strong>HTML 5</strong>.</li>
<li>Build an <strong>App Generator</strong> (e.g. ePUB, RSS, audiobooks) selling this generated apps.</li>
<li>Became a specialist building <strong>Augmented Reality Layers</strong> for <a href="http://layar.com/">Layar</a> or <a href="http://www.wikitude.org/">Wikitude</a></li>
</ul>
<p>Mark has done an incredible job posting these business models, you can read the different post for more information (<a href="http://www.androidguys.com/2009/09/14/40bizmodels/">Part 1</a>, <a href="http://www.androidguys.com/2009/09/15/40-android-business-models-part-two/">2</a>, <a href="http://www.androidguys.com/2009/09/16/40-android-business-models-part-three/">3</a>, <a href="http://www.androidguys.com/2009/09/17/40-android-business-models-part-four/">4</a>, <a href="http://www.androidguys.com/2009/09/18/40-android-business-models-part-five/">5</a> and <a href="http://androidheadlines.com/2010/03/android-business-models.html">6</a>)</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.androidsx.com%2Fhow-to-make-money-with-android-business-models%2F&amp;linkname=How%20to%20make%20money%20with%20Android%20%26%238211%3B%20business%20models"><img src="http://www.androidsx.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a><h3  class="related_post_title">Related posts</h3><ul class="related_post"><li><a href="http://www.androidsx.com/6-things-weve-learned-from-the-android-market/" title="6 things we&#8217;ve learned from the Android market">6 things we&#8217;ve learned from the Android market</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.androidsx.com/how-to-make-money-with-android-business-models/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to write untestable code</title>
		<link>http://www.androidsx.com/how-to-write-untestable-code/</link>
		<comments>http://www.androidsx.com/how-to-write-untestable-code/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 10:08:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[software quality]]></category>

		<guid isPermaLink="false">http://www.androidsx.com/?p=144</guid>
		<description><![CDATA[Say that we write code for a machine that it so powerful that rumors say it could generate a black hole. We need to be serious about security: we cannot let those evil international terrorists find out our black-hole-making secrets and blow half the Milky Way Galaxy.
If they managed to access our source code, they [...]]]></description>
			<content:encoded><![CDATA[<p>Say that we write code for a machine that it so powerful that <a href="http://www.foxnews.com/story/0,2933,538447,00.html?FORM=ZZNR3">rumors say it could generate a black hole</a>. We need to be serious about security: we cannot let those evil international terrorists find out our black-hole-making secrets and blow half the Milky Way Galaxy.</p>
<p>If they managed to access our source code, they could extend it and adapt it to their cruel use case, which would represent a huge threat for the humankind. In order to reduce risks, we should write code that is difficult to understand, to maintain and of course, to test. Here&#8217;s how:</p>
<ul>
<li><strong>Depend on concrete classes</strong> &#8211; Tie things down to concrete classes &#8211; avoid interfaces wherever possible: they let people substitute the concrete classes you&#8217;re using for their own classes which would implement the same contract in the interface. By depending on concrete implementation, we make sure that they&#8217;ll have a hard time testing whether their evil plans will release enough energy to toast their breakfast bread.</li>
<li><strong>Make your own dependencies</strong> &#8211; Instantiate objects using new in the middle of methods, don&#8217;t pass the object in. Anyone that wants to test that code is forced to use that concrete object you new&#8217;ed up: they can&#8217;t inject a dummy, fake, or other mock in to simplify the behavior or make assertions about what you do to it.</li>
<li><strong>Conditional slalom</strong> &#8211; Feel good when writing lengthy if branches and switch statements. These increase the number of possible execution paths that tests will need to cover when exercising the code under test. The higher the cyclomatic complexity, the harder it is to test and understand! Why use polymorphism instead of conditionals? Don&#8217;t make it so easy! Make the branching both deep and wide: if you&#8217;re not consistently going at least 5 conditionals deep, the patient terrorists might even find out what the code intends to do under all possible combinations.
<p>It is also known as the Arrow Antipattern:</p>
<pre class="brush:xml">
if a
    if b
        if c
            foo();
        else
            bar();
        endif
    endif
endif
</pre>
<p>Refactoring into something more suitable is usually possible: </p>
<pre class="brush:xml">
if a &#038;&#038; b
    if c
        foo();
    else
        bar();
    endif
endif
</pre>
</li>
<li><strong>Use global flags</strong> &#8211; Why call a method explicitly? Set a flag in one part of your code, in order to cause an effect in a totally different part of your application. The testers will go crazy trying to figure out why all of a sudden a conditional that was evaluating true one minute is all of a sudden evaluating to false. Not to mention the mess that using that system becomes: if we manage to keep the wikis safe from their hands, they&#8217;ll have a very hard time figuring out what system properties to set in order to get the deadly weapon working.</li>
<li><strong>Loop-switch sequence</strong> &#8211; By means of which a clear set of steps is implemented as a byzantine switch-within-a-loop. Fulfilling our mission to obfuscate the code , is much more difficult to decipher the intent and actual function of the code than the more straightforward refactored solution.
<p>This is a terrorist-safe code snippet:</p>
<pre class="brush:java">
// parse a key, a value, then three parameters
String key = null;
String value = null;
final List<String> params = new LinkedList<String>();

for (int i = 0; i < 5; i++) {
  switch (i) {
    case 0: key = stream.parse(); break;
    case 1: value = stream.parse(); break;
    default: params.add(stream.parse()); break;
  }
}
</pre>
<p>While this is too easy to understand:</p>
<pre class="brush:java">
// parse a key and value
final String key = stream.parse();
final String value = stream.parse();

// parse 3 parameters
final List<String> params = new LinkedList<String>();
for (int i = 0; i < 3; i++) {
  params.add(stream.parse());
}
</pre>
</li>
</ul>
<p><em>References: <a href="http://googletesting.blogspot.com/2008/07/how-to-write-3v1l-untestable-code.html">This post</a> on the <a href="http://googletesting.blogspot.com/">Google Testing Blog</a>, the <a href="http://c2.com/cgi/wiki?ArrowAntiPattern">Arrow AntiPattern</a> in <a href="http://c2.com/">Cunningham &#038; Cunningham</a> webpage.</em></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.androidsx.com%2Fhow-to-write-untestable-code%2F&amp;linkname=How%20to%20write%20untestable%20code"><img src="http://www.androidsx.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a><h3  class="related_post_title">Related posts</h3><ul class="related_post"><li><a href="http://www.androidsx.com/dont-look-for-things-ask-for-them/" title="Don&#8217;t look for things. Ask for them!">Don&#8217;t look for things. Ask for them!</a></li><li><a href="http://www.androidsx.com/cant-test-that-singleton-try-dependency-injection/" title="Can&#8217;t test that Singleton? Try Dependency Injection!">Can&#8217;t test that Singleton? Try Dependency Injection!</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.androidsx.com/how-to-write-untestable-code/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>6 things we&#8217;ve learned from the Android market</title>
		<link>http://www.androidsx.com/6-things-weve-learned-from-the-android-market/</link>
		<comments>http://www.androidsx.com/6-things-weve-learned-from-the-android-market/#comments</comments>
		<pubDate>Mon, 14 Dec 2009 11:08:48 +0000</pubDate>
		<dc:creator>Omar Pera Mira</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[android market]]></category>
		<category><![CDATA[apps]]></category>

		<guid isPermaLink="false">http://www.androidsx.com/?p=85</guid>
		<description><![CDATA[We have three applications on the Android Market, AnyRSS reader widget (0.50 EUR), FML F*ck my life widget (Free) and Daily Stuff widget (Free). It makes for more than 60000 downloads, being F*ck my life the most downloaded application among them. We want to share our experience with the market for the last several months:

According [...]]]></description>
			<content:encoded><![CDATA[<p>We have three applications on the Android Market, <a title="AnyRSS reader widget" href="http://www.androidsx.com/apps/anyrss-reader-widget/" target="_self">AnyRSS reader widget</a> (0.50 EUR), <a title="FML F*ck my life widget" href="http://www.androidsx.com/apps/fml-fck-my-life-widget/" target="_self">FML F*ck my life widget</a> (Free) and <a title="Daily Stuff widget" href="http://www.androidsx.com/apps/daily-stuff-widget/" target="_self">Daily Stuff widget</a> (Free). It makes for more than 60000 downloads, being F*ck my life the most downloaded application among them. We want to share our experience with the market for the last several months:</p>
<ul>
<li>According to our <a href="http://www.flurry.com/">Flurry</a> stats, the <strong>best day to update an application</strong> is <strong>Friday morning</strong> (European afternoon or evening). On other days, such as Wednesday or Thursday, you usually get more downloads. However, other developers don&#8217;t usually release new versions in the weekends, so your application stays longer in the top of the <em>Just in</em> section.</li>
</ul>
<p><img class="aligncenter size-full wp-image-87" title="flurry" src="http://www.androidsx.com/wp-content/uploads/2009/12/flurry.png" alt="flurry" width="531" height="369" /></p>
<ul>
<li>At the moment, Android users <strong>are not accostumed to paying for an application</strong>. So, if you want to make money with your application, which strategy should you follow? Free and paid version? Free with ads? Our experience with a paid version is not quite as motivating as we expected (just a few hundreds Euros since September). Free applications with ads have more users but usually less money. The decision depends on many factors, and there&#8217;s no magical answer. We&#8217;ll probably do an experiment with our paid application, making it free application and adding some <a href="http://www.admob.com/">AdMob</a> ads (<a href="http://www.google.com/ads/mobileapps/developers.html">Google Adsense</a><strong> </strong>is only available for apps with 100K daily pageviews).</li>
<p></p>
<li>It is important to have <strong>good ratings during the first days</strong> as a new application in te market. Bad comments and ratings in the first days will make it difficult to start growing and get enough relevance to appear on relatively top places in the search results. Make sure your application is pleasant to use from the beginning: focus on reliability and stability rather than on advanced features. <strong>You do not want to annoy your first users</strong>.</li>
<p></p>
<li>Add as many <strong>relevant keywords in the description</strong> that identify your application as needed: it is too optimistic to expect people to search specifically for your application name.</li>
<p></p>
<li>Most of the users don&#8217;t care about the impact of a bad rating. If there&#8217;s any small problem, they will just throw in a bad rating/comment and uninstall the app. On the other hand, we must <strong>take care of the lovely users that suggest new features, report bugs, etc.</strong>.</li>
<p></p>
<li>Be careful with your update frequency: <strong>it is annoying for users if you update too frequently</strong>. In the Android market, <strong>each update goes to the <em>Just in</em> section</strong>, which has been really controversial in the Android world. There are many developers that abuse this by making frequent updates to get more downloads. On the other hand, getting visibility on the market through the search or the rankings is not easy, so updating is <strong>one of the best ways to gain more downloads</strong>: but with <em>real</em> updates, don&#8217;t be evil!.</li>
<p>
</ul>
<p>In other posts, we will talk about the limitations of the Android market. <strong>If we are to compete with the <a href="http://www.apple.com/iphone/apps-for-iphone/" target="_blank">App Store</a></strong>, the market should be one of the main worries for Android Core Developers and <strong>learn from its competitors</strong>. Yes, the UI is better and what not, but the visibility, the <strong><em>search</em></strong> and the average money (check <a href="http://www.locationrevolution.com/stats/SkyhookDevelopersSurveyDec2009.pdf" target="_blank">this study</a>) that you get with your applications is much lower than in the App Store. With tech companies such as Motorola investing a lot of resources in Android, <strong>there will be really powerful market competitors</strong> such as <a href="http://android-france.fr/2009/12/12/shop4apps-la-place-de-marche-dapplications-de-motorola/" target="_blank">SHOP4APPS</a> (seen in <a title="El androide libre" href="http://www.elandroidelibre.com/2009/12/filtraciones-en-motorola-opus-one-y-un.html?utm_source=feedburner&amp;utm_medium=feed&amp;utm_campaign=Feed%3A+blogspot%2FbARA+%28Androide+Libre%29" target="_blank">El Androide libre</a>, in Spanish).</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.androidsx.com%2F6-things-weve-learned-from-the-android-market%2F&amp;linkname=6%20things%20we%26%238217%3Bve%20learned%20from%20the%20Android%20market"><img src="http://www.androidsx.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a><h3  class="related_post_title">Related posts</h3><ul class="related_post"><li><a href="http://www.androidsx.com/how-to-make-money-with-android-business-models/" title="How to make money with Android &#8211; business models">How to make money with Android &#8211; business models</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.androidsx.com/6-things-weve-learned-from-the-android-market/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t look for things. Ask for them!</title>
		<link>http://www.androidsx.com/dont-look-for-things-ask-for-them/</link>
		<comments>http://www.androidsx.com/dont-look-for-things-ask-for-them/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 11:04:07 +0000</pubDate>
		<dc:creator>Pablo Pera Mira</dc:creator>
				<category><![CDATA[software quality]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.androidsx.com/?p=75</guid>
		<description><![CDATA[Before our previous article, our code to model a mechanic used to look like this:
class Mechanic {
    private final Engine engine = ServiceLocator.getCar().getEngine();
    public void fixEngine() { /* ... */ }
}

Luckily, we applied some dependency injection, resulting in nicer code: testable, and with an explicit dependency on the ServiceLocator:
class [...]]]></description>
			<content:encoded><![CDATA[<p>Before our previous article, our code to model a mechanic used to look like this:</p>
<pre class="brush:java">class Mechanic {
    private final Engine engine = ServiceLocator.getCar().getEngine();
    public void fixEngine() { /* ... */ }
}
</pre>
<p>Luckily, we applied some dependency injection, resulting in nicer code: testable, and with an explicit dependency on the <code>ServiceLocator</code>:</p>
<pre class="brush:java">class Mechanic {
    private final Engine engine;
    public Mechanic(final ServiceLocator serviceLocator) {
        engine = serviceLocator.getCar().getEngine();
    }
    public void fixEngine() { /* ... */ }
}
</pre>
<p>However, does our mechanic care about the <code>ServiceLocator</code> at all? Not really, it doesn&#8217;t even store a reference to it! <strong>The mechanic just wants an engine</strong>.</p>
<p>There are some problems with this kind of code:</p>
<ul>
<li>The <code>ServiceLocator</code> probably has references to a lot of other classes in the system. By the transitive property, our <code>Mechanic</code> class is too coupled with the rest of the system: if you want to reuse it in a different project, you&#8217;d need to reference not only <code>Mechanic</code> and <code>Engine</code>, but also the <code>ServiceLocator</code> with all its dependencies.</li>
<li>The API is not clear. An API client knows that it&#8217;d need to provide the <code>ServiceLocator</code> (already an advantage respect to the Singleton-based case). What it doesn&#8217;t know is what the mechanic really needs (an engine). This fact is hidden in the source code.</li>
<li>The tests contain a lot of setup junk that masks its real purpose. For every test of the mechanic, you&#8217;d need to mock the ServiceLocator, and then make sure the appropriate reference can be retrieved from it (the <code>Engine</code>, in this case). When we test the class <code>Garage</code>, that needs a <code>Mechanic</code>, we&#8217;ll have to replicate this tedious setup there too.</li>
</ul>
<p>This is how the test looks like now:</p>
<pre class="brush:java">
@Test
public void testCantFixBrokenDownEngine() {
    final Engine engine = EngineFactory.buildBrokenEngine();
    final Car mockCark = Mockito.mock(Car.class);
    Mockito.when(mockCar.getEngine()).thenReturn(engine);
    final ServiceLocator mockServiceLocator = Mockito.mock(ServiceLocator.class);
    Mockito.when(mockServiceLocator.getCar()).thenReturn(car);
    final Mechanic mechanic = new Mechanic(mockServiceLocator);
    mechanic.fixEngine();
    assertFalse(engine.works());
}
</pre>
<p><strong>Why not just ask for what you need?</strong></p>
<pre class="brush:java">
class Mechanic {
    private final Engine engine;
    public Mechanic(final Engine engine) {
        this.engine = engine;
    }
    public void fixEngine() { /* ... */ }
}
</pre>
<pre class="brush:java">
@Test
public void testCantFixBrokenDownEngine() {
  Engine engine = EngineFactory.buildBrokenEngine();
  Mechanic mechanic = new Mechanic(engine);
  mechanic.fixEngine();
  assertFalse(engine.works());
}
</pre>
<p><strong>Everyone wins:</strong></p>
<p>API writers:</p>
<ul>
<li>Unaffected by ServiceLocator changes</li>
<li>In-code documentation is easier to write (what was your comment on the <code>Mechanic</code>&#8217;s constructor for the <code>ServiceLocator</code>)?</li>
</ul>
<p>Test writers:</p>
<ul>
<li>Tests are easy to read</li>
<li>Very little setup code</li>
<li>Unaffected by <code>ServiceLocator</code> changes</li>
</ul>
<p>API users:</p>
<ul>
<li>The API is clear: the mechanic needs an engine</li>
</ul>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.androidsx.com%2Fdont-look-for-things-ask-for-them%2F&amp;linkname=Don%26%238217%3Bt%20look%20for%20things.%20Ask%20for%20them%21"><img src="http://www.androidsx.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a><h3  class="related_post_title">Related posts</h3><ul class="related_post"><li><a href="http://www.androidsx.com/cant-test-that-singleton-try-dependency-injection/" title="Can&#8217;t test that Singleton? Try Dependency Injection!">Can&#8217;t test that Singleton? Try Dependency Injection!</a></li><li><a href="http://www.androidsx.com/how-to-write-untestable-code/" title="How to write untestable code">How to write untestable code</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.androidsx.com/dont-look-for-things-ask-for-them/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Can&#8217;t test that Singleton? Try Dependency Injection!</title>
		<link>http://www.androidsx.com/cant-test-that-singleton-try-dependency-injection/</link>
		<comments>http://www.androidsx.com/cant-test-that-singleton-try-dependency-injection/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 20:40:06 +0000</pubDate>
		<dc:creator>Pablo Pera Mira</dc:creator>
				<category><![CDATA[software quality]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.androidsx.com/?p=60</guid>
		<description><![CDATA[So you want to test this method:

public class Client {

    public int process(Params params) {
        final Server server = Server.getInstance();
        final Data data = server.retrieveDate(params);
        // do stuff
    [...]]]></description>
			<content:encoded><![CDATA[<p>So you want to test this method:</p>
<pre class="brush:java">
public class Client {

    public int process(Params params) {
        final Server server = Server.getInstance();
        final Data data = server.retrieveDate(params);
        // do stuff
    }
}
</pre>
<p>We don&#8217;t want to retrieve an instance of a real server for our little unit-test, so how can we test this method?</p>
<p><strong>It is hard to test code that uses singletons.</strong></p>
<p>We don&#8217;t control the creation of the singleton object, as it is performed inside a static method. There is no way to mock the object in order to test the behavior of our method in isolation.</p>
<p><strong>Refactor it to use Dependency Injection.</strong></p>
<p>You can refactor <code>Client</code> to avoid using the singleton pattern. Instead of obtaining the Server instance from the static <code>getInstance()</code> method, allow <code>Client</code> to accept it through its constructor.</p>
<pre class="brush:java">
public class Client {
    private final Server server;  

    public Client(Server server) {
        this.server = server;
    }  

    public int process(Params params) {
        final Data data = server.retrieveData(params);
        // do stuff
    }
}
</pre>
<p>Let&#8217;s write that test now:</p>
<pre class="brush:java">
@Test
public void testConnectionUpTime() {
    final Server mockServer = Mockito.mock(Server.class);
    final Params params = // ...
    Mockito.when(mockServer.process(params)).thenReturn(5);
    final Client client = new Client(mockServer);
    assertEquals(5, client.process(params));
}
</pre>
<p><strong>The code is now both clearer and testable.</strong></p>
<p>The dependency between the client and the server is now explicit: <code>Client client = new Client(server);</code>. There is no way a developer creates a client instance without noticing that a server instance must be configured: it is a parameter in the constructor.</p>
<p>The singleton allowed to create a client instance without configuring the server in advance. The object would be successfully created and the application would execute, until one of the methods runs into a non-configured/non-reachable/null server and fail at runtime :&#8217;-(</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.androidsx.com%2Fcant-test-that-singleton-try-dependency-injection%2F&amp;linkname=Can%26%238217%3Bt%20test%20that%20Singleton%3F%20Try%20Dependency%20Injection%21"><img src="http://www.androidsx.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a><h3  class="related_post_title">Related posts</h3><ul class="related_post"><li><a href="http://www.androidsx.com/dont-look-for-things-ask-for-them/" title="Don&#8217;t look for things. Ask for them!">Don&#8217;t look for things. Ask for them!</a></li><li><a href="http://www.androidsx.com/how-to-write-untestable-code/" title="How to write untestable code">How to write untestable code</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.androidsx.com/cant-test-that-singleton-try-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello android world!</title>
		<link>http://www.androidsx.com/hello-world/</link>
		<comments>http://www.androidsx.com/hello-world/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 12:50:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.androidsx.com/?p=1</guid>
		<description><![CDATA[This is where we share our experience with the Android platform. Also, find articles about software quality and testing that we will write in a weekly basis.


This is where we share our experience with the Android platform. Also, find articles about software quality and testing that we will write in a weekly basis.


Random PostsHow to [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: left;">This is where we share our experience with the Android platform. Also, find articles about software quality and testing that we will write in a weekly basis.</p>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;">
<blockquote style="border: medium none ; margin: 0pt 0pt 0pt 40px; padding: 0px;">
<div>This is where we share our experience with the Android platform. Also, find articles about software quality and testing that we will write in a weekly basis.</div>
</blockquote>
</div>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.androidsx.com%2Fhello-world%2F&amp;linkname=Hello%20android%20world%21"><img src="http://www.androidsx.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a><h3  class="related_post_title">Random Posts</h3><ul class="related_post"><li><a href="http://www.androidsx.com/how-to-write-untestable-code/" title="How to write untestable code">How to write untestable code</a></li><li><a href="http://www.androidsx.com/dont-look-for-things-ask-for-them/" title="Don&#8217;t look for things. Ask for them!">Don&#8217;t look for things. Ask for them!</a></li><li><a href="http://www.androidsx.com/how-to-make-money-with-android-business-models/" title="How to make money with Android &#8211; business models">How to make money with Android &#8211; business models</a></li><li><a href="http://www.androidsx.com/cant-test-that-singleton-try-dependency-injection/" title="Can&#8217;t test that Singleton? Try Dependency Injection!">Can&#8217;t test that Singleton? Try Dependency Injection!</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.androidsx.com/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
