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

  • Home
  • SEARCH
  • 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 4069426
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:29:59+00:00 2026-05-20T16:29:59+00:00

There are several methods of unit testing in Android, what’s the best one for

  • 0

There are several methods of unit testing in Android, what’s the best one for testing a custom view I’ve written?

I’m currently testing it as part of my activity in an instrumentation test case, but I’d rather test just the view, isolated.

  • 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-05-20T16:30:00+00:00Added an answer on May 20, 2026 at 4:30 pm

    As mentioned in wikibooks:

    unit testing is a method by which individual units of source code are tested to determine if they are fit for use.

    So when you say you want to test your custom view, you can check various methods of your custom views like "onTouchEvent", "onDown", "onFling", "onLongPress", "onScroll", "onShowPress", "onSingleTapUp", "onDraw" and various others depending on your business logic. You can provide mock values and test it. I would suggest two methods of testing your custom view.

    1) Monkey Testing

    Monkey testing is random testing performed by automated testing tools.

    G.D.S. Prasad on geekinterview.com

    and:

    A monkey test is a unit test that runs with no specific test in mind. The monkey in this case is the producer of any input. For example, a monkey test can enter random strings into text boxes to ensure handling of all possible user input or provide garbage files to check for loading routines that have blind faith in their data.

    sridharrganesan on geekinterview.com

    This is a black box testing technique and it can check your custom view in so many unique conditions that you will get astonished 🙂 .

    2) Unit Testing

    2a) Use Robotium Unit Testing Framwork

    Go to Robotium.org or http://code.google.com/p/robotium/ and download the example test project. Robotium is a really easy to use framework that makes testing of android applications easy and fast. I created it to make testing of advanced android applications possible with minimum effort. Its used in conjunction with ActivityInstrumentationTestCase2.

    2b) Use Android Testing Framework

    Here are the links to the reference:
    http://developer.android.com/reference/android/test/ActivityInstrumentationTestCase2.html
    and
    http://developer.android.com/reference/android/test/ActivityUnitTestCase.html

    For starters:
    http://developer.android.com/guide/topics/testing/testing_android.html

    According to one user : Aside from easily testing non platform
    dependent logic I haven’t found a
    clever way to run tests, so far (at
    least for me) any actual platform
    logic testing is cumbersome. It’s
    almost non trivial anyway because I’ve
    found differences in implementation
    between the emulator and my actual
    device and I hate to run a unit test
    implementation on my device just to
    remove the application afterwards.

    My strategy has been: Try to be
    concise and make the logic well
    thought out and then test
    implementation piece by piece (less
    then desirable).

    Also Stephen Ng provides good aproach for real Unit Test for Android projects solution: https://sites.google.com/site/androiddevtesting/

    One user has made a screencast.

    Here’s a ScreenCast I made on how I got Unit Tests to work. Simple Unit
    Tests and more complex unit tests that
    depend on having a reference to
    Context or Activity objects.
    http://www.gubatron.com/blog/2010/05/02/how-to-do-unit-testing-on-android-with-eclipse/

    Hope it helps you testing your custom view in all possible conditions 🙂


    Comment (futlib) All your suggestions seem to involve testing the ACTIVITY, while I really want to test just the VIEW. I might want to use this view in other activities, so it doesn’t make much sense for me to test it with a specific one. – futlib

    Answer: To implement a custom view,
    you will usually begin by providing
    overrides for some of the standard
    methods that the framework calls on
    all views. For example "onDraw",
    "onKeyDown(int, KeyEvent)",
    "onKeyUp(int, KeyEvent)",
    "onTrackballEvent(MotionEvent)" etc of
    your custom view. So when you want to
    do unit testing for your custom you’ll
    have to test these methods, and
    provide mock values to it so that you
    can test your custom view on all
    possible cases. Testing these methods
    doesn’t mean that you are testing your
    ACTIVITY, but it means testing your
    custom view (methods/functions) which
    is within an activity. Also you’ll
    have to put your custom view in an
    Activity eventually for your target
    users to experience it. Once
    thoroughly tested , your custom view
    can be placed in many projects and
    many activities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If there are several methods in the test class, I found that the order
There are several different methods for converting floating point numbers to Integers in JavaScript.
There are several filter methods for dates (year,month,day). If I want to match a
I have a class that currently has several methods that take integer parameters. These
There are several people about (myself being one of them) who have been developing
There are several methods on country codes. I have a list of codes with
I am working on a legacy code where there are several methods with calls
Ok. So there are several methods to implement Collision detection like R-trees, Quadtrees, BSP
I have concrete class that I want to mock. There are several annotated methods
There are several types of objects in a system, and each has it's own

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.