Android technology for Mock Data Source in activity unit testing

I am new to unit testing, and I was learning how to use the jUnit framework for android (using ActivityInstrumentationTestCase2), but I had problems developing how I insert a false data source and activity, for example:

In activiy i have this

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.market_screen);
        ListView products = (ListView)findViewById(R.id.product_list);
        MarketsAdapter adapter = new  MarketsAdapter(this, new ProductDataSource());
        products.setAdapter(adapter);

}

I am currently passing ProductDataSource to an adapter that connects to a web service to pull products for the adapter. In my tests, I do not want to connect to the web service. What is the best technique for injecting a mock data source into activity for testing? Should I create a ProductDataSource in the application instance and then use MockApplication in my tests to create a mock data source?

thank

, setUp(): ListView Mock setAdapter(MockDataSource). runOnUiThread().

mActivity = getActivity();
mDataSource = new FakeDataSource();     
mMarketsListView = (ListView)mActivity.findViewById(R.id.product_list);
mActivity.runOnUiThread(
      new Runnable() {
        public void run() {
          mMarketsListView.setAdapter(new MarketsAdapter(mActivity,mDataSource));

        } // end of run() method definition
   } // end of anonymous Runnable object instantiation
); // 
+5
1

, "Mocking" , . , .

, , , . , unit test, ProductDatasource, Mocking, "mock" , . .Net Java, JUnit JMock , .

, "" . , - , , .

, , ProductDatasource :

public interface IProductsDatasource {

    public List<Product> getProducts();

}

:

public class ProductsDatasource implements IProductsDatasource {

    private List<Product> mProducts;

    public ProductsDatasource(List<Product> products) {
        mProducts = products;
    }

    public List<Product> getProducts() {
        return mProducts;
    }

}

, - , TargetAdapter, ProductDatasource. ProductDatasource, . unit test ProductDatasource. , ProductDatasource .

public class TargetAdapter {

    private IProductsDatasource mDatasource;

    public TargetAdapter(IProductsDatasource datasource) {
        mDatasource = datasource;
    }

    public List<Product> products() {
        return mDatasource.getProducts();
    }

}

, , , , .

@Test
public void TargetAdapterReturnsProducts() {

    List<Product> data = new ArrayList<Product>();
    data.add(new Product("Sample Product 1"));
    data.add(new Product("Sample Product 2"));
    data.add(new Product("Sample Product 3"));


    TargetAdapter adapter = new TargetAdapter(new ProductsDatasource(data)); // See the dependency
    List<Product> products = adapter.products();

    Assert.assertNotNull(adapter);
    Assert.assertTrue(products.size() == 3);
}

, , . , , , . Mocking , , , . .

, , JMock :

@Test
public void MockingTest() {

    final Mockery context = new Mockery();

    final List<Product> mockData = new ArrayList<Product>();
    mockData.add(new Product("Sample Product 1"));
    mockData.add(new Product("Sample Product 2"));
    mockData.add(new Product("Sample Product 3"));

    final IProductsDatasource mockDatasource = context.mock(IProductsDatasource.class);

    context.checking(new Expectations(){{
        oneOf (mockDatasource).getProducts(); will(returnValue(mockData));  // This is where I tell JMock to return my test data when getProducts() is called
    }});

    TargetAdapter adapter = new TargetAdapter(mockDatasource); // No dependency ;)
    List<Product> products = adapter.products();

    Assert.assertNotNull(adapter);
    Assert.assertTrue(products.size() == 3);

}

, , Mock . mock-, , . , , , , . , , , refactor โ†’ extract

eclipse, , , . , !

+7

All Articles