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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:50:20+00:00 2026-05-30T22:50:20+00:00

I have a JUnit test case in an Android project that contains code that

  • 0

I have a JUnit test case in an Android project that contains code that looks like this:

private static final URI TEST_RESOURCE_URL = TasksService.TASKLIST_RESOURCELIST_URL.resolve("task/test.task");

public void setUp () {
    Log.i("Test", "TEST_RESOURCE_URL=" + TEST_RESOURCE_URL);
}

This test class has multiple test methods, some of which refer to (but do not attempt to modify) the value of this constant. However, when I run these tests (Android 2.2.2), all of these tests but the first one fails, and logcat shows me this:

03-03 18:56:41.791: I/Test(12008): TEST_RESOURCE_URL=http://apate.meridiandigital.net/tasks/task/test.task
03-03 18:56:42.101: I/Test(12008): TEST_RESOURCE_URL=null
03-03 18:56:42.131: I/Test(12008): TEST_RESOURCE_URL=null
03-03 18:56:42.151: I/Test(12008): TEST_RESOURCE_URL=null
03-03 18:56:42.281: I/Test(12008): TEST_RESOURCE_URL=null
03-03 18:56:42.311: I/Test(12008): TEST_RESOURCE_URL=null
03-03 18:56:42.341: I/Test(12008): TEST_RESOURCE_URL=null
03-03 18:56:42.361: I/Test(12008): TEST_RESOURCE_URL=null
03-03 18:56:42.391: I/Test(12008): TEST_RESOURCE_URL=null
03-03 18:56:42.391: I/Test(12008): TEST_RESOURCE_URL=null

How does a static final field change value like this? How do I prevent this from happening? Are there other situations where it might happen?

— EDIT 1

I have now trimmed the code down to a smaller example that can be included in its entirety. See below:

public class MyService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}


public class StaticFinalTest extends ServiceTestCase<MyService> {
    public StaticFinalTest() {
        super(MyService.class);
    }

    public static final Object CONST2 = new Object();

    public void testA ()
    {
        assertNotNull (CONST2);
    }

    public void testB ()
    {
        assertNotNull (CONST2);
    }
}

When this test runs, testA passes but testB fails. If testA is commented out, testB passes.

It seems to be important that it is a ServiceTestCase. A standard JUnit TestCase does not cause the problem. If ‘CONST2’ is a String, both tests pass as expected. Any other reference type seems to reproduce the problem.

  • 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-30T22:50:21+00:00Added an answer on May 30, 2026 at 10:50 pm

    It appears that AndroidTestCase uses reflection to set all non-primitive fields to null after each test in scrubClass. It doesn’t check to see if the fields are static or final, so this seems to be the source of the problem.

    To solve it, change the field to non-final and set it inside setUp. Also, make sure you call super.setUp() as the first line of your setUp to make sure the test case is properly initialized.

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

Sidebar

Related Questions

I have a JUnit test that fails because the milliseconds are different. In this
I have this JUnit test that I need help developing a Interface and Class
I have a JUnit test class in which I have several static final int
I have a JUnit 4 test case with the Spring @Transactional annotation that saves
I have a Junit4 test case which statically imports the org.junit.Assert.assertEquals method(s). import static
I have a JUnit test that tests adding Strings to a Dictionary custom type.
Normally I would have one junit test that shows up in my integration server
I have a little JUnit-Test that export an Object to the FileSystem. In the
So I have a test case that I want to make into a thread.
I have the following test case in eclipse, using JUnit 4 which is refusing

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.