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 4578150
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:26:42+00:00 2026-05-21T20:26:42+00:00

I have been having quite a bit of trouble implementing unit testing on the

  • 0

I have been having quite a bit of trouble implementing unit testing on the Android. As a simple test, I’ve been trying to match a string retrieved from string resources:

String myString = myActivity.getResources().getString(R.string.testString));

However, when unit testing this invariably results in a null pointer exception. This includes robolectric as well as the Junit implementation delivered with the Android sdk.

One possible solution is to approach the retrieval of resources in a manner similar to a data access object. That is, create an interface through which string resources would be accessed. This would allow me to mock access the string resource. Similarly, I could separate the non-android dependent behavior of, say, an Activity, into a separate pojo class. This would allow me to run unit tests using standard Java testing tools. In fact, I could potentially delegate any Android infrastructure related activity to an interface.

This seems like a lot of jumping through hoops to get to unit testing. Is it worth it? Is there a more viable approach?

  • 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-21T20:26:43+00:00Added an answer on May 21, 2026 at 8:26 pm

    It turned out, the problem was that the activity has to be gotten in the actual test method. So, for example, my method now looks like this:

    public void testGetActivityResourceString() {
    
        Activity myActivity = this.getActivity();
        String myString = myActivity.getResources().getString(R.string.hello);
        Assert.assertNotNull(myString);
    }
    

    Whereas before I was creating activity in setup. This giveaway was in the docs:

    “For each test method invocation, the Activity will not actually be created until the first time this method is called.”

    This was a real hassle to figure out. The example for HelloWorldTest doesn’t work for the same reason.

    Here’s the full entry:

    Public T getActivity ()
    Since: API Level 3
    Get the Activity under test, starting it if necessary.

    For each test method invocation, the Activity will not actually be created until the first time this method is called.

    If you wish to provide custom setup values to your Activity, you may call setActivityIntent(Intent) and/or setActivityInitialTouchMode(boolean) before your first call to getActivity(). Calling them after your Activity has started will have no effect.

    NOTE: Activities under test may not be started from within the UI thread. If your test method is annotated with UiThreadTest, then your Activity will be started automatically just before your test method is run. You still call this method in order to get the Activity under test.

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

Sidebar

Related Questions

I have to compute at libraries quite often these days, and have been having
I have been having some problems trying to get my PHP running. When I
I have been creating Unit tests like crazy and find that I'm often having
I have been trying to tackle this problem , but I am having difficulty
I having trouble in dividing the HTML frames. I have been using the following
I have been having some issues with LINQ-To-SQL around memory usage. I'm using it
This is a really weird problem that I have been having. When I download
We have been using CruiseControl for quite a while with NUnit and NAnt. For
Have been looking at the MVC storefront and see that IQueryable is returned from
Have been studying the file system related classes of Adobe AIR 1.5, but so

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.