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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:04:24+00:00 2026-06-11T16:04:24+00:00

I’m trying to write unit tests for my android application and I want to

  • 0

I’m trying to write unit tests for my android application and I want to mock my service class. I want to test some error behaviors in the service, such as connection errors or file not found.

To simplify my question, I started a new Android Project and created an Activity and a Service class:

MyAndroidProjectActivity.java

package br.org.venturus.android;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.TextView;
import br.org.venturus.android.service.MyService;

public class MyAndroidProjectActivity extends Activity {
    private MyService service;
    private ServiceConnection mConnection = new ServiceConnection() {

        public void onServiceConnected(ComponentName className, IBinder binder) {
            service = ((MyService.MyBinder) binder).getService();

        }

        public void onServiceDisconnected(ComponentName className) {
            service = null;
        }
    };
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        {
            Intent service = new Intent(this, MyService.class);
            startService(service);

            bindService(service, mConnection, Context.BIND_AUTO_CREATE);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // stop MyService
        {
            Intent svc = new Intent(this, MyService.class);
            stopService(svc);
        }
    }

    public void doChange(View view) {
        TextView tv = (TextView) findViewById(R.id.tvHello);

        tv.setText(service.getNewString());
    }
}

MyService.java

package br.org.venturus.android.service;

import br.org.venturus.android.R;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

public class MyService extends Service {

    private IBinder binder;

    public MyService() {
        this.binder = new MyBinder();
    }
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    public class MyBinder extends Binder {
        public MyService getService() {
            return MyService.this;
        }
    }

    public String getNewString() {
        return getString(R.string.change);
    }

}

And I created a new Android Test Project with this unit class:

MyAndroidProjectActivityTest.java

package br.org.venturus.android.test;

import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.Button;
import android.widget.TextView;
import br.org.venturus.android.MyAndroidProjectActivity;
import br.org.venturus.android.R;

public class MyAndroidProjectActivityTest extends
        ActivityInstrumentationTestCase2<MyAndroidProjectActivity> {

    private MyAndroidProjectActivity mActivity;
    private TextView tvHello;
    private Button btChange;

    public MyAndroidProjectActivityTest() {
        super("br.org.venturus.android", MyAndroidProjectActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        // Get the activity and components
        {
            mActivity = this.getActivity();
            tvHello = (TextView) mActivity
                    .findViewById(br.org.venturus.android.R.id.tvHello);
            btChange = (Button) mActivity
                    .findViewById(br.org.venturus.android.R.id.btChange);
        }

    }

    public void testPreconditions() {
        assertNotNull(tvHello);
        assertNotNull(btChange);
    }

    @UiThreadTest
    public void testChangeButton() {
        assertEquals(tvHello.getText(), getActivity().getString(R.string.hello));
        btChange.performClick();
        assertEquals(tvHello.getText(), getActivity()
                .getString(R.string.change));
    }
}

When I run the unit tests, the pre conditions and the change button tests are fine. What I want to do, for instance, is to create a MyServiceMock and override the MyService.getNewString to return other value, and mock this object in the test.

Something like this:

MyServiceMock.java

package br.org.venturus.android.test.service;

import br.org.venturus.android.service.MyService;

public class MyServiceMock extends MyService {
    @Override
    public String getNewString() {      
        return "This is the new value!";
    }
}

Is that possible?

  • 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-11T16:04:26+00:00Added an answer on June 11, 2026 at 4:04 pm

    Yes, it’s possible and I think the example of the MyServiceMock.java above is also an alternative.

    There are a few mocking frameworks out there that you can use for this.

    I have some experience with Mockito.

    You can just mock the MyService class and specify that when the getNewString method is called, return a string “This is the new value!”.

    MyService myServiceMock = Mockito.mock(MyService.class);
    
    Mockito.doReturn("This is the new value!").when(myServiceMock).getNewString();
    

    You can also use the Mockito.doThrow() method to throw an exception when a mock method is called:

    Mockito.doThrow(new RuntimeException()).when(mockedList).clear();
    
    //following throws RuntimeException:
    mockedList.clear();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into

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.