Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8123107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:04:01+00:00 2026-06-06T06:04:01+00:00

Hї! I have wrote test for my application. I need add item to database

  • 0

Hї!

I have wrote test for my application. I need add item to database throught UI interface (using robotium) and then I want to check if item exists in database using SQLiteDatabase.

Item is added succesfully (I see new record in database after test finished), but isExistsInDb in my test class returns false. I do not understand why. Could you please help me.
Thanks!

Activity class:

public abstract class EditActivity {


   // Some code .....

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initButtonCancelOk();
    }

    protected void validateAndSave() {
        try {
            formValidator.validateAll();
            if (formValidator.isFormValid()) {
                DatabaseOpenHelper doh = new DatabaseOpenHelper(this);
                Dao d = new Dao(doh);
                d.add(fetchObjectFromUi());
                finish(); // destroy this activity
            } else {
                ToastImage.makeImageText(context,
                        R.drawable.warning,
                        formValidator.getMessages(),
                        Toast.LENGTH_SHORT
                    ).show();
            }
        } catch (Exception e) {
            Toast.makeText(context, " Error during validate form ", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }

    private void initButtonCancelOk() {
        btnOk = (Button) findViewById(R.id.btn_ok);
        btnOk.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                validateAndSave();
            }
        });
    }    
}

Test class:

public class AddItemSmokeTest extends extends ActivityInstrumentationTestCase2<EditActivity> {

    protected Activity activity;
    protected Solo solo;

    public AddItemSmokeTest() {
        super("com.myapp", EditActivity.class);
        Intent i = createIntent(2);
        setActivityIntent(i);
        activity = getActivity();
        solo = new Solo(getInstrumentation(), activity);
        solo.sleep(1000); // interval between tests
    }

    protected Intent createIntent(long transType) {
        Intent i = new Intent();
        i.putExtra(INTENT_VALUE_MODE_NAME, MODE_INSERT_TRANSACTION);
        i.putExtra(INTENT_VALUE_TYPE_ID_NAME, transType);
        return i;
    }

    @Override
    protected void tearDown() throws Exception {

    }

    protected void setIncomExpenseData(AbsTransIncomeExpenseTestData testData) {
        solo.pressSpinnerItem(CATEGORY_SPN_INDEX, testData.getCategorySpinnerPos());
        solo.pressSpinnerItem(ACCOUNT_SPN_INDEX, testData.getAccountSpinnerPos());
        solo.typeText((EditText) activity.findViewById(com.rirdev.moneycounter.R.id.et_sum), testData.getSum());
        solo.typeText((EditText) activity.findViewById(com.rirdev.moneycounter.R.id.et_comment), testData.getComment());
    }

    @Smoke
    public void testAddIncomeTransaction() throws Exception {
        initForType(TransactionType.INCOME);
        AbsTransIncomeExpenseTestData testData = new IncomeTestData();
        setIncomExpenseData(testData);
        solo.clickOnButton(OK);
        //solo.getActivityMonitor();
        assertTrue( 
                    "Item" + testData.getComment() + " was not added ",
                    isExistsInDb(activity, Transactions.TABLE_NAME, Transactions.DESCRIPTION, testData.getComment())
                  );
    }

    protected static boolean isExistsInDb(Context context, String tableName, String commentFieldName, String comment) {

        DatabaseOpenHelper doh = new DatabaseOpenHelper(context);
        SQLiteDatabase db = doh.getDatabaseReadable();
        Cursor cursor = null;
        try {
            String query = "SELECT COUNT(*) FROM " + tableName + " WHERE " + commentFieldName + " = \"" + comment + "\"";
            cursor = db.rawQuery(query, null);
            cursor.moveToFirst();
            if (cursor.getInt(0) > 1) {
                return true;
            }
            return false;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            db.close();
            doh.close();
        }

    }

}

Update:

If I run test the second time it is passed because in database exists item added by previous test.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:04:02+00:00Added an answer on June 6, 2026 at 6:04 am

    I recommend to use use parametrized statement, your approach is danger and not much clear.
    Also much better is use getCount() method.

    String query = "SELECT COUNT(*) FROM " + tableName + " WHERE columnName = ?";
    cursor = db.rawQuery(query, new Sring[] {comment});
    int count = 0;
    if (cursor.getCount() > 0) {
       cursor.moveToFirst();
       count = cursor.getInt(0);
    }
    if (count > 0) {
       return true;
    }
    else {
       return false;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem I have a UDPlistener application that I need to write a unit test
I wrote a small test program for flipperview. I have 3 views. I call
I wrote a sliding list for a project. Here it is: http://study-wise.appspot.com/test/left_right.html I have
guys, I wrote a function to test if two inputs (a and b) have
I have a slow MySQL query in my application that I need to re-write.
I need to autheticate on a site using forms authentication and then redirect the
I have an application that I would like to write an automated test for.
For an application, I need to do a load test for a web service
I have a application that need event handling on a unusual way. For my
I have a web application that I wrote in Visual Studio 2008 that targets

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.