在Android上进行仪器测试

时间:2018-07-17 00:09:32

标签: android testing

Android上的仪器测试。我在进行此测试时应该检查什么。我已经尝试过一个样本,但是我感到困惑。我也有两个方法的Java类。这些方法具有Context作为参数,它们在一个片段中被调用。我已经尝试过InstrumentationRegistry.getTargetContext()来获取上下文。还是我出错了。

这是我的Java类:

    public class MarkerSize {
  /**
   * Convert a SVG into a BitmapDescriptor for map marker
   *
   * @param context     The main Activity
   * @param vectorResId The ID of the SVG
   * @return The map marker as a BitmapDesriptor

   */

  public BitmapDescriptor bitmapDescriptorFromVector ( Context context, int vectorResId ) {
    try {
      Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorResId);

      vectorDrawable.setBounds(0,
              0,
              vectorDrawable.getIntrinsicWidth(),
              vectorDrawable.getIntrinsicHeight());
      Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
              vectorDrawable.getIntrinsicHeight(),
              Bitmap.Config.ARGB_8888);
      Canvas canvas = new Canvas(bitmap);
      vectorDrawable.draw(canvas);
       BitmapDescriptor bp= BitmapDescriptorFactory.fromBitmap(bitmap);
      return bp;
    }catch ( Resources.NotFoundException e ){
      return null;
    }
  }

  /**
   * Change the size of the marker
   * @param context MapsActivity
   * @param iconName The name of the SVG file
   * @param width The desired width for the icon
   * @param height The desired heigh for the icon
   * @return A Bitmap representation of the SVG
   */
  public Bitmap resizeMapIcons (Context context, String iconName, int width, int height ) {
    Bitmap imageBitmap = BitmapFactory.decodeResource (context.getResources (),
                                                       context.getResources ().getIdentifier
                                                           (iconName,
                                                            "drawable",
                                                            context.getPackageName ()));
    Bitmap resizedBitmap = Bitmap.createScaledBitmap (imageBitmap, width, height, false);
    return resizedBitmap;
  }
}

这是我的简短测试:

@RunWith(JUnit4.class)
@SmallTest
public class testMarkersmethods {

  MapViewFragment mapViewFragment;
  private int VECTORID= R.drawable.ic_dest_pin;
  Context context=InstrumentationRegistry.getTargetContext();
  MarkerSize markerSize=new MarkerSize();
  BitmapDescriptor bitmapDescriptorrr ;

  @Test
  public void test_bitMapfromVector(){
    if(context!=null) {
      BitmapDescriptor bitmapDescriptor = markerSize.bitmapDescriptorFromVector(context, VECTORID);
      Assert.assertNotNull(bitmapDescriptor);
      Assert.assertEquals(bitmapDescriptorrr, bitmapDescriptor);
    }
  }
}

我收到此日志:

java.lang.NullPointerException: IBitmapDescriptorFactory is not initialized
at com.google.android.gms.common.internal.Preconditions.checkNotNull(Unknown Source:8)
at com.google.android.gms.maps.model.BitmapDescriptorFactory.zzf(Unknown Source:4)
at com.google.android.gms.maps.model.BitmapDescriptorFactory.fromBitmap(Unknown Source:2)
at algorithm.MarkerSize.bitmapDescriptorFromVector(MarkerSize.java:38)
at com.example.giheok.streetview.testMarkersmethods.test_bitMapfromVector(testMarkersmethods.java:33)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)

0 个答案:

没有答案