【问题标题】:unit testing FaceDetector Google mobile vision java.lang.UnsupportedOperationException单元测试 FaceDetector 谷歌移动视觉 java.lang.UnsupportedOperationException
【发布时间】:2019-02-01 08:50:47
【问题描述】:

我正在为涉及来自谷歌移动视觉库的 FaceDetector 的类方法设置单元测试,但我得到了 java.lang.UnsupportedOperationException,但它在真正的 android 设备上运行时有效。是否可以测试 FaceDetector 代码?谢谢

public class HeadDetector
{

    private Bitmap facePicture;

    private Context context;

    private RectF headRectangle=new RectF();

    public HeadDetector(Bitmap facePicture,Context context) {

        this.facePicture = facePicture;
        this.context = context;
    }


    public RectF generateHeadRectangle() {

        FaceDetector faceDetector = new
            FaceDetector.Builder(context).setTrackingEnabled(false)
            .build();

    Frame frame = new Frame.Builder().setBitmap(facePicture).build();
    // when unit test hit below code
    //java.lang.UnsupportedOperationException
    SparseArray<Face> faces = faceDetector.detect(frame);

    if(faces.size()>0){

        Face face = faces.get(0);

        headRectangle = new RectF(0,0,face.getWidth(),face.getHeight());
    }

    return headRectangle;

  }

}

public class HeadDetectorTest  {

    @Test
    public void shouldGenerateHeadRectangle(){

    Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();

    HeadDetector headDetector = new HeadDetector( TestHelper.createBitmapTest(context), context);

    RectF rectF = headDetector.generateHeadRectangle();

    assertNotEquals(rectF.width(),0.0f);

    assertNotEquals(rectF.height(),0.0f);

    assertThat(rectF.height(),greaterThan(rectF.width()));

}

}

【问题讨论】:

    标签: java android unit-testing junit4 robolectric


    【解决方案1】:

    UnsupportedOperationException 很可能是由于尝试打开不可用的硬件摄像头造成的。因此,您可以使用 @RequiresDevice 注释测试:

    @RequiresDevice
    public class HeadDetectorTest  {
        ...
    }
    

    表示不应在模拟器上运行特定测试。

    仅当测试在物理 android 设备上运行时才会执行。

    将网络摄像头连接到模拟器,以便为其提供所需的内容。

    只需将网络摄像头聚焦在某个头部的图像上,以提供示例数据:)

    【讨论】:

      猜你喜欢
      • 2019-11-24
      • 2017-03-20
      • 2017-07-26
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 2019-12-18
      • 2018-04-07
      • 2017-06-30
      相关资源
      最近更新 更多