title: Android unit test: mock BluetoothDevice and write to parcel
body: <p>I would like to ask about unit testing a class that has <code>BluetoothDevice</code> variable. </p>
  
  <p>My object is a simple object that contains some primitive variable and a BluetoothDevice variable. My object is also parcelable.</p>
  
  <p>Eventually my code works totally fine on the mobile device but I have strange error when I run my unit tests.</p>
  
  <p>I mocked the BluetoothDevice in the test class as shown below:</p>
  
  <pre><code>@RunWith(RobolectricTestRunner.class)
  @Config(manifest=Config.NONE)
  public class BluetoothDeviceTest {
  
      @Mock
      BluetoothDevice device1;
  
      @Before
      public void initMocks(){
          MockitoAnnotations.initMocks(this);
  
          when(device1.getAddress()).thenReturn("01:02:03:04:05:06");
          when(device1.getName()).thenReturn("device767b1");
          when(device1.getBondState()).thenReturn(BOND_NONE);
      }
  }
  </code></pre>
  
  <p>In my object among other primitives I use:</p>
  
  <ul>
  <li><p>I use <code>out.writeParcelable(mBluetoothDevice,0);</code> inside <code>@Override public void writeToParcel(Parcel out, int flags)</code> method.   </p></li>
  <li><p>I use <code>mBluetoothDevice = in.readParcelable(BluetoothDevice.class.getClassLoader());</code> inside <code>protected MyObject(Parcel in)</code> constructor.</p></li>
  </ul>
  
  <p>The unit test that tests parcelling of my object instance fails with an exception
  <code>java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String</code></p>
  
  <p>Once again please note that when my code runs totally fine and parcelling works good in the mobile app I work on. Only unit test is acting strange.</p>
  
  <p>I suspect this is because my mocked <code>BluetoothDevice</code> variable is shorter in the parcel than a normal real instance and the order of the data fields in the parcel gets messed up. </p>
  
  <p>Has anybody unit tested with a mocked <code>BluetoothDevice</code> and can give me some hint ? Thank you</p>