I'm trying to test a ContentProvider class, and can't make it work.
getProvider() keeps returning null, but as I understand from the ProviderTestCase2.setUp() code, it shouldn't.
public class NotesProviderTest extends ProviderTestCase2<NotesProvider>
{
...
public NotesProviderTest()
{
super(NotesProvider.class, Contract.AUTHORITY);
}
#Override
protected void setUp() throws Exception
{
super.setUp();
}
public void testNoteProvider__inserts_a_valid_record() throws Exception
{
Note note = new Note(new JSONObject(simpleNoteJson));
NotesProvider provider = getProvider();
Uri insert = provider.insert(Note.URI, note.getContentValues());
assertEquals(1L, ContentUris.parseId(insert));
Cursor cursor = provider.query(Note.URI, null, null, new String[]{}, null);
assertNotNull(cursor);
cursor.close();
}
}
Side note: the provider works if used within the app.
Thanks in advance.
I just ran into this issue myself. You need to tell AndroidJUnit4 to run the setUp method with the #Before annotation. If you don't do this the setUp method will not be called before your unit test runs.
The code snippet for overriding the setUp method on http://developer.android.com/training/testing/integration-testing/content-provider-testing.html is misleading and doesn't mention that you need an #Before annotation.
Try the following:
#Before
#Override
public void setUp() throws Exception
{
setContext(InstrumentationRegistry.getTargetContext());
super.setUp();
}
As part of the setUp() method a MockContentResolver should be created. Use this to create and inject the provider.
See class MockContentResolver:
http://developer.android.com/reference/android/test/mock/MockContentProvider.html
Source of example:
http://alvinalexander.com/java/jwarehouse/android/test-runner/src/android/test/ProviderTestCase2.java.shtml
Partial sample from example in link above:
#Override
protected void setUp() throws Exception {
super.setUp();
mResolver = new MockContentResolver();
final String filenamePrefix = "test.";
RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
new MockContext2(), // The context that most methods are delegated to
getContext(), // The context that file methods are delegated to
filenamePrefix);
mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);
mProvider = mProviderClass.newInstance();
mProvider.attachInfo(mProviderContext, null);
assertNotNull(mProvider);
mResolver.addProvider(mProviderAuthority, getProvider());
}
Related
I've started learning android unit tests, but it looks very hard to find some good guides or information. Every example have a stupid example about 2+2 = 4
Say I write a little SDK which has few functions
MySdk.Init(Context context)
MySdk.CallTask()
I create an androidTest file
How should I call my SDK functions to check how they work? Somewhere required parameters like int/string/context. I just really don't understand, please help me.
This is what I've tried
public class AndroidTest {
private Activity context;
//default test
#Test
public void addition_correct() throws Exception {
assertEquals(4, 2 + 2);
}
#Test
public void checkContext() {
context = getActivity();
assertNotNull(context);
}
#Test
public void testInitPhase() {
MySdk.Init(context, new SdkInitializationListener() {
#Override
public void onInitializationSuccessful(String adv_id) {
assert (adv_id != null);
}
#Override
public void onInitializationError() {
}
});
}
}
For context i was tried context = new mockContext();. It's passed as context = null and my SDK failed with initialization.
Unit tests are mainly about testing an individual class in isolation, so that you can check if individual public methods of a class behave as you intend them to, and continue to do so if you change that class' code in the future. Let's say you have a class like this:
public class UtilityFunctions {
public int double(int value) {
return value * 2;
}
public String mirror(String value) {
if (value == null) return "";
return value + new StringBuilder(value).reverse().toString();
}
}
You want to test these two methods with:
valid input values, and check the output is as expected
invalid values, and check that errors are handled accordingly (and the correct exceptions thrown if necessary)
So a test class for the above class may look like this
#RunWith(JUnit4.class)
public class UtilityFunctionsTest {
private UtilityFunctions utility;
#Before
public void setUp() {
// Initialises any conditions before each test
utility = new UtilityFunctions();
}
#Test
public void testDoubleFunction() {
assertEquals(2, utility.double(1));
assertEquals(8, utility.double(4));
assertEquals(-12, utility.double(-6));
assertEquals(0, utility.double(0));
}
#Test
public void testMirror() {
assertEquals("", utility.mirror(null));
assertEquals("", utility.mirror(""));
assertEquals("aa", utility.mirror("a"));
assertEquals("MirrorrorriM", utility.mirror("Mirror"));
}
}
These standard Java unit tests are run from the test directory. However, you'll need to run tests in the androidTest directory whenever you're using Android-specific classes such as Context. If you're creating a MockContext, you're simply creating an empty Context whose methods don't do anything.
Without me knowing anything about what your MySDK does, I think you may need to pass a fully-functioning Context into your class for your tests. The Android JUnit runner does provide this with InstrumentationRegistry.getTargetContext(), so for your example, you may need to add this #Before method:
#Before
public void setUp() {
context = InstrumentationRegistry.getTargetContext();
}
You'll also need to remove the context = getActivity(); line from your first test.
I've tried everything already. The startActivity are not receiving the intent. Here's the code:
public class ColaboradorTeste extends ActivityUnitTestCase<ColaboradorMainActivity> {
private UserDAO userDAO;
private ColaboradorMainActivity activity;
public ColaboradorTeste() {
super(ColaboradorMainActivity.class);
}
#Override
protected void setUp() throws Exception{
super.setUp();
startActivity(new Intent(getInstrumentation().getTargetContext(), ColaboradorMainActivity.class), null, null);
activity = (ColaboradorMainActivity)getActivity().getApplicationContext();
userDAO = new UserDAO(activity);
}
public void testBase() throws Exception{
...
}
}
Here's the error:
java.lang.NullPointerException
at br.fsw.seatafmobile.ColaboradorTeste.setUp(ColaboradorTeste.java:23)
I know the problem is in the startActivity, but I really don't know how to solve it.
If anyone could help I'll really appreciate.
I finally found the solution!
My test is using a database created on the app. So I had to change the Build Variants from Unit Test to Android Instrumentation Tests.
I have a little problem figuring out how to test my Activity using Robolectric 2.2. I am probably not correctly setting up the lifecycle or the entire test...
In my Activity, I have a Otto producer like this:
#Produce public GetAlarmList produceAlarmList() {
Log.v(this, "Producing GetAlarmList event.");
return new GetAlarmList(mAlarmList);
}
The following is my test.
#RunWith(RobolectricTestRunner.class)
public class GLarmListFragmentTests {
//GLarmMain mActivity;
ActivityController<GLarmMain> mController;
#Before
public void setUp() throws Exception {
mController = Robolectric.buildActivity(GLarmMain.class);
}
#After
public void tearDown() throws Exception {
mController = mController.destroy();
}
#Test
public void shouldHaveListFragment() {
GLarmMain activity = mController.create().start().resume().visible().get();
GLarmListFragment listFragment = (GLarmListFragment) activity.getSupportFragmentManager().findFragmentById(R.id.main_list_frag);
assertNotNull(listFragment);
}
#Test
public void shouldHaveListAdapter() {
GLarmMain activity = mController.create().start().resume().visible().get();
GLarmListFragment listFragment = (GLarmListFragment) activity.getSupportFragmentManager().findFragmentById(R.id.main_list_frag);
FuncAlarmListAdapter listAdapter = (FuncAlarmListAdapter)listFragment.getListAdapter();
assertNotNull(listAdapter);
}
}
Every time I launch it, I receive:
java.lang.IllegalArgumentException: Producer method for type class ar.android.app.glarm.events.GetAlarmList found on type class ar.android.app.glarm.ui.GLarmMain, but already registered by type class ar.android.app.glarm.ui.GLarmMain.
at com.squareup.otto.Bus.register(Bus.java:198)
at ar.android.app.glarm.ui.GLarmMain.onResume(GLarmMain.java:461)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$6.run(ActivityController.java:216)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
at org.robolectric.util.ActivityController.invokeWhilePaused(ActivityController.java:214)
at org.robolectric.util.ActivityController.resume(ActivityController.java:152)
at ar.android.app.glarm.test.GLarmListFragmentTests.shouldHaveListAdapter(GLarmListFragmentTests.java:51)
Does someone have the same issue? How can I solve it?
Found the problem, leaving it here for reference.
I was not handling the pause()/onPause() life-cycle step and therefore never calling:
#Override
protected void onPause() {
super.onPause();
Log.v(this, ".onPause()");
mBus.unregister(this); // unregisters.
}
Changing the above code as following solved:
#After
public void tearDown() throws Exception {
mController = mController.pause().stop().destroy();
}
I'm testing database using ProviderTestCase2 super class. Here is my code snippet:
public class MyProviderTest extends ProviderTestCase2<MyProvider>{
private static MockContentResolver resolver;
private static IsolatedContext context;
public MetaDataProviderTest() {
super(MyProvider.class, Provider.AUTHORITY);
}
#Override
protected void setUp() throws Exception {
try{
super.setUp();
resolver = getMockContentResolver();
} catch(Exception e){
}
}
}
public void testfirst(){
Cursor cursor = resolver.query(ProviderContract.Channels.CHANNEL_URI,null,null,null,null);
....
}
}
When I debug the above code Im getting the passed result. When I run it, I get Null cursor implying there is no such table as channel. Please help in solving this. Where did I go wrong?
There was race condition. Introduced delay in Setup(). It's working fine. But I don't know whether this is the exact solution.
you should write the code :
MockContentResolver.addProvider(authority, yourprovider);
then have a try
I'm new at JUnit tests, I'm trying to test database access through this code:
public class SeuticketTest extends ActivityInstrumentationTestCase2<Seuticket> {
private Seuticket mActivity;
public SeuticketTest(String name) {
super("br.com.code.seuticket.android.view",Seuticket.class);
setName(name);
}
#Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();
}
public void testTicketInsertion() {
Ticket ticket = new Ticket("123453", "Vip", new Date(), "Vila Country");
PersistenceTicket persistence = new PersistenceTicket(mActivity);
persistence.addTicket(ticket);
assertEquals(persistence.fetchTicket(ticket.getTicketCode()).getTicketCode(),ticket.getTicketCode());
}
public void testUserInsertion() {
User user = new User();
user.setPin("1234");
user.setPhone("9241173");
PersistenceUser persistence = new PersistenceUser(mActivity);
persistence.addUser(user);
assertEquals(persistence.fetchUser().getPin(), user.getPin());
}
}
But my tests get stuck after complete the testTicketInsertion, the second test keep running forever, and sometimes before it run the tests this message shows at the console:
Test run failed: Process crashed.
Any ideas?
Hope to find an answer here.
Thanks people!
I am brand new to JUnit testing in Android and ran into the same problem. Make sure you have the tearDown method overridden and are making the call to super.
protected void tearDown() throws Exception {
super.tearDown();
}