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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:58:00+00:00 2026-06-10T09:58:00+00:00

I am slowly becoming obsessed with unit testing. I am trying to develop as

  • 0

I am slowly becoming obsessed with unit testing. I am trying to develop as much software as I can using test-driven development. I am using JUnit to unit test my android applications.

I have been working on an app that uses bluetooth and am having a hard time unit testing it. I have an Activity that uses BluetoothAdapter to obtain a list of paired and discovered devices. Although it works, I would like to know how to unit test it.

To get the list of paired devices, I call getBondedDevices() on the instance of BluetoothAdapter. The problem is I don’t know how to stub or mock this method (or any other bluetoothAdapter method that my Activity calls) so I can’t test my Activity against different lists of paired devices.

I thought about using Mockito or trying to subclass BluetoothAdapter to somehow stub out the methods I’m interested in, but it’s a final class so I can’t do either.

Any ideas on how I could test programs that use BluetoothAdapter or other resources that are (as far as I know) difficult or impossible to stub or mock? As another example, how would you test a program that uses sockets?

thanks in advance for any help

aleph_null

  • 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-10T09:58:02+00:00Added an answer on June 10, 2026 at 9:58 am

    To test your activity, you may refactor your code. Introduce a BluetoothDeviceProvider with a default implementation.

    public interface BluetoothDeviceProvider {
        Set<BluetoothDevice> getBluetoothDevices();
    }
    
    public class DefaultBluetoothDeviceProvider implements BluetoothDeviceProvider {
        public Set<BluetoothDevice> getBluetoothDevices() {
            return new BluetoothAdapter.getDefaultAdapter().getBondedDevices();
        }
    }
    

    then inject this new interface, in the activity :

    public class MyActivity extends Activity {
        private BluetoothDeviceProvider bluetoothDeviceProvider;
    
        public MyActivity(BluetoothDeviceProvider bluetoothDeviceProvider) {
            this.bluetoothDeviceProvider = bluetoothDeviceProvider;
        }
    
        protected void onStart() {
            Set<BluetoothDevice> devices = bluetoothDeviceProvider.getBluetoothDevices();
            ... 
        }
        ...
    }
    

    Now the activity seems unit-testable. But the BluetoothDevice is still final and you can’t inject a mock in your activity. So you have to refactor this new code and introduce a new interface that wraps the BluetoothDevice… -> an abstraction layer upon the core android classes.

    At the end the activity behavior can be checked via various unit tests… So the implementations of the newly introduced interfaces remains to test. To do this, you can :

    • keep them not (unit) tested, not a big problem for me since they just do delegation

    • look at PowerMock.

    Also check this wiki page about mocking final classes using PowerMock.

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

Sidebar

Related Questions

Lastly I've been feeling like jQuery is slowly becoming the defacto standard in JS
I am slowly getting used to using the Qt4 GUI framework. In a project
I'm being slowly driven mad at work having to manually gather data contained within
I have been slowly learning iPhone development and seem to keep hitting walls where
I'm slowly learning mod_rewrite (keyword is slowly) and am using a tutorial's line of
I'm trying to slowly knock out all of the intricacies of python. Basically, I'm
I'm slowly learning PHP, MySQL, along with some HTML, using localhost as my webserver.
I'm getting nowhere slowly using tfssecurity.exe to programatically give a user the retainindefinitly permission
I'm slowly building my application to a working state. I'm using two functions called
I'm slowly getting through my exceptions on my code using GWT + Hibernate, my

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.