Archive for May, 2012

An open letter to an IT recruitment consultant

Dear IT Recruitment Consultant,

For the purposes of this letter, I’m going to assume that you are one of the tiny minority of members of your profession who have some integrity and ability. Unfortunately for you, you have chosen a career with a deservedly dreadful reputation. Most IT recruitment consultants are no better than a combination of pimp and double glazing salesman.

But you’re not one of this majority. You can have a conversation with me where I don’t need to explain everything to you in words of one syllable. Repeatedly. And you have a unique source of amazing candidates unavailable to any other recruitment consultant and who would be just perfect for me.

So how do you get to talk to me?

Well this is where it gets difficult I’m afraid. You know that you’re one of this tiny minority, but I don’t. And unfortunately all of you claim that you’re different from all the rest. How do I tell the difference between someone who really does have a brain and the vast majority who just claim to?

Well, I’m sorry, but there is exactly one way to achieve this, and that’s to be good at your job. It’s a long, slow path, but eventually someone I trust will recommend that I talk to you. And if I need to, I will.

What you absolutely do not do is call me. Or e-mail me. Or try to link to me on LinkedIn (especially not by lying and claiming to be my friend or an ex-colleague in order to avoid paying LinkedIn). Or try to get in contact in any other way whatsoever.

My problem is not that I don’t know enough recruitment consultants. Nobody who has spent more than a few years in IT can possibly fail to know many (many!) recruitment consultants. I have found a small set that I trust, respect, and will work with again (you see – I do know that some of you are different).

I don’t want to be introduced to any more. There are too damn many of you, and most of you aren’t worth even the 60 seconds of my time it takes to tell you to go away. Not that it ever takes 60 seconds, because you always have to followup and tell me why I’m wrong about you and you’re different from all the rest. And ignoring you doesn’t work either, because you’ll just try again. And again. And again.

I will never do business with any recruitment consultant who cold calls. Ever.

So please stop? Please?

Mockito on Android step-by-step

One of my biggest frustrations with writing code for Android has been the fact that none of the current Java mocking frameworks work on Android’s Dalvik VM. I tried to address that myself by getting ScalaMock working on Android, but that’s been less successful than I hoped (primarily due to general problems with getting Scala working in the Android environment).

Happily there’s been a major breakthrough recently – Mockito, one of the established Java mocking frameworks now supports Android 🙂 In this article, I’ll go through getting an Android application up and running with tests written using Mockito.

This is an Android version of the example in Martin Fowler’s article Mocks Aren’t Stubs. The code is checked into GitHub here. You’ll need to have the Android SDK and ADT Plugin for Eclipse installed to run this code.

We’re going to build a (very) simple ordering system. Orders will succeed if there’s enough inventory in our warehouse and fail if not. Let’s start by creating a very simple little Android application for us to test:

  1. Create a new Android project in Eclipse called WarehouseManager. Select the “Create a Test Project” option.
  2. The core abstraction is a warehouse, represented by a Warehouse interface:

    package com.example;
    
    public interface Warehouse {
        boolean hasInventory(String product, int quantity);
        void remove(String product, int quantity);
    }
  3. And here’s a very simple concrete implementation of Warehouse:

    package com.example;
    
    import java.util.HashMap;
    
    public class RealWarehouse implements Warehouse {
    
        public RealWarehouse() {
            products = new HashMap();
            products.put("Talisker", 5);
            products.put("Lagavulin", 2);
        }
    
        public boolean hasInventory(String product, int quantity) {
            return inStock(product) >= quantity;
        }
    
        public void remove(String product, int quantity) {
            products.put(product, inStock(product) - quantity);
        }
    
        private int inStock(String product) {
            Integer quantity = products.get(product);
            return quantity == null ? 0 : quantity;
        }
    
        private HashMap products;
    }
  4. We remove things from the warehouse by placing an Order:

    package com.example;
    
    public class Order {
    
        public Order(String product, int quantity) {
            this.product = product;
            this.quantity = quantity;
        }
    
        public void fill(Warehouse warehouse) {
            if (warehouse.hasInventory(product, quantity)) {
                warehouse.remove(product, quantity);
                filled = true;
            }
        }
    
        public boolean isFilled() {
            return filled;
        }
    
        private boolean filled = false;
        private String product;
        private int quantity;
    }
  5. We’ll need a UI to allow us to make orders, so modify main.xml to look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
      <TextView  
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:text="Product:"
          />
      <EditText
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content"
          android:id="@+id/product"
          />
      <TextView
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:text="Quantity:"
          />
      <EditText
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content"
          android:id="@+id/quantity"
          />
      <Button
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:text="Place order"
          android:onClick="placeOrder" />
    </LinearLayout>
  6. And finally, here’s the implementation of WarehouseManagerActivity:

    package com.example;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class WarehouseManagerActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            productEditText = (EditText)findViewById(R.id.product);
            quantityEditText = (EditText)findViewById(R.id.quantity);
        }
    
        public void placeOrder(View view) {
            String product = productEditText.getText().toString();
            int quantity = Integer.parseInt(quantityEditText.getText().toString());
            Order order = new Order(product, quantity);
            order.fill(warehouse);
    
            String message = order.isFilled() ? "Success" : "Failure";
            Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
            toast.show();
        }
    
        private Warehouse warehouse = new RealWarehouse();
    
        private EditText productEditText;
        private EditText quantityEditText;
    }

You should now have a little Android application that can be compiled and installed with ant install. Here’s what it looks like:

Screenshot

So, now that we’ve got something to test, let’s test it:

  1. The version of Mockito that supports Android has not yet been released, so you’ll need to get the source and build it for yourself. After building, you should have mockito-all-1.9.1-SNAPSHOT.jar in the target directory.
  2. Create a libs directory underneath WarehouseManagerTest and copy mockito-all-1.9.1-SNAPSHOT.jar into it. Download the latest dexmaker into the same directory.
  3. Finally, we can write our tests, which create mock instances of the Warehouse interface:

    package com.example.test;
    
    import android.test.InstrumentationTestCase;
    import com.example.*;
    
    import static org.mockito.Mockito.*;
    
    public class OrderTest extends InstrumentationTestCase {
    
        public void testInStock() {
            Warehouse mockWarehouse = mock(Warehouse.class);
    
            when(mockWarehouse.hasInventory("Talisker", 50)).thenReturn(true);
    
            Order order = new Order("Talisker", 50);
            order.fill(mockWarehouse);
    
            assertTrue(order.isFilled());
            verify(mockWarehouse).remove("Talisker", 50);
        }
    
        public void testOutOfStock() {
            Warehouse mockWarehouse = mock(Warehouse.class);
    
            when(mockWarehouse.hasInventory("Talisker", 50)).thenReturn(false);
    
            Order order = new Order("Talisker", 50);
            order.fill(mockWarehouse);
    
            assertFalse(order.isFilled());
        }
    }
  4. Run your tests 🙂