Skip to content

Instantly share code, notes, and snippets.

@dadamssg
Created May 12, 2013 21:49
Show Gist options
  • Save dadamssg/5565043 to your computer and use it in GitHub Desktop.
Save dadamssg/5565043 to your computer and use it in GitHub Desktop.
this is my test
import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.util.Modules;
import com.vurchase.core.ColumnType;
import com.vurchase.core.DataSource;
import com.vurchase.core.SQLiteHelper;
import com.vurchase.injected.LawnBizProdModule;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import roboguice.RoboGuice;
import roboguice.activity.RoboActivity;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
@RunWith(RobolectricTestRunner.class)
public class DataSourceTest {
protected Application application = mock(Application.class, RETURNS_DEEP_STUBS);
protected Context context = mock(RoboActivity.class, RETURNS_DEEP_STUBS);
protected SQLiteHelper dbHelper = mock(SQLiteHelper.class, RETURNS_DEEP_STUBS);
protected CustomerSchema schema = mock(CustomerSchema.class,RETURNS_DEEP_STUBS);
protected SQLiteDatabase database = mock(SQLiteDatabase.class, RETURNS_DEEP_STUBS);
@Before
public void setUp() {
RoboGuice.setBaseApplicationInjector(application, RoboGuice.DEFAULT_STAGE,
Modules.override(RoboGuice.newDefaultRoboModule(application)).with(new DataSourceTestModule()));
when(context.getApplicationContext()).thenReturn(application);
when(application.getApplicationContext()).thenReturn(application);
//set up the record schema
String[] columnNames = { "_id", "created_at", "updated_at", "name" };
ColumnType[] columnTypes = { ColumnType.INTEGER, ColumnType.INTEGER, ColumnType.INTEGER, ColumnType.TEXT };
when(schema.getColumnNames()).thenReturn(columnNames);
when(schema.getColumnTypes()).thenReturn(columnTypes);
when(schema.getTable()).thenReturn("customers");
//don't know what to do here, when the DataSource.save() method gets run, the the third argument of insert() won't be null
when(database.insert("customers",null,null)).thenReturn(Long.valueOf(4));
}
@After
public void teardown() {
// Don't forget to tear down our custom injector to avoid polluting other test classes
RoboGuice.util.reset();
}
@Test
public void testDatasourceIsTestIsWorking(){
DataSource dataSource = RoboGuice.getInjector(context).getInstance(DataSource.class);
Customer customer = mock(Customer.class, RETURNS_DEEP_STUBS);
when(customer.getSchema()).thenReturn(schema);
when(customer.getValue("_id")).thenReturn(0);
when(customer.getValue("created_at")).thenReturn(0);
when(customer.getValue("updated_at")).thenReturn(0);
when(customer.getValue("name")).thenReturn("Test Name");
assertNotNull("Datasource test is running", "");
}
public class DataSourceTestModule extends AbstractModule {
@Override
protected void configure() {
bind(SQLiteHelper.class).toInstance(dbHelper);
bind(CustomerSchema.class).toInstance(schema);
bind(SQLiteDatabase.class).toInstance(database);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment