首页 > 解决方案 > 断言 - 内部调用问题

问题描述

大家好,我的第一个测试有一些问题

我正在写这个片段,但我一直遇到这个问题:

java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86) at org.junit.Assert.assertTrue(Assert.java:41) at org.junit.Assert.assertNotNull(Assert.java:712)在 org.junit.Assert.assertNotNull(Assert.java:722)

有人知道热帮我吗?也许在这堂课中测试哪些是正确的?多谢

我已经在 gradle 中实现了 Junit

测试类是:

    public class QrActivityTest {

        public QrActivity tester;
        @Before
        public void setUp(){
            tester = new QrActivity();
        }
        @Test
        public void onCreate() {
            //barcodeDetector = new BarcodeDetector.Builder(tester.getApplicationContext()).setBarcodeFormats(Barcode.QR_CODE).build();
           // Assert.assertNotNull(barcodeDetector);
            Assert.assertNotNull(tester);
            Assert.assertNotNull(tester.cameraSource);

        }}

课程是:

    public class QrActivity extends AppCompatActivity {

        SurfaceView surfaceView;
        CameraSource cameraSource;
        TextView textView;
        BarcodeDetector barcodeDetector;

        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.barcode_scanner_layout);
            surfaceView = (SurfaceView) findViewById(R.id.camera);
            textView = (TextView) findViewById(R.id.txt);

            barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();

            cameraSource = new CameraSource.Builder(this, barcodeDetector).setRequestedPreviewSize(640, 480).setAutoFocusEnabled(true).build();

            surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                    if (ActivityCompat.checkSelfPermission(QrActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                        return;
                    }
                    try {
                        cameraSource.start(holder);
                    } catch (IOException e) {
                        Toast.makeText(QrActivity.this, "errore fotoamera", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }

                }


//this is to take data


                @Override
                public void receiveDetections(Detector.Detections<Barcode> detections) {
                    final SparseArray<Barcode> qrCodes = detections.getDetectedItems();
                    if (qrCodes.size() != 0) {
                        textView.post(new Runnable() {
                            @Override
                            public void run() {
                                Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                                vibrator.vibrate(300);
                                textView.setText(qrCodes.valueAt(0).displayValue);
                                if (qrCodes.valueAt(0).displayValue.equals("129063")) {
                                    Intent intent = new Intent(QrActivity.this, AttrezzaturaRecycleView.class);
                                    startActivity(intent);
                                    Utility.showToast(QrActivity.this, "Dispositivo trovato!");
                                }
                            }
                        });

    }

标签: javaandroidjunitjunit4

解决方案


断言检查内部值。如果不满足断言,它们会抛出异常。因此,您可以检查完整的堆栈跟踪。

该异常可能发生在该行中Assert.assertNotNull(tester.cameraSource);。这是因为 cameraSource 将为空。这是因为您只创建了一个类的实例,但没有onCreate调用类似的事件。一个简单的单元测试不足以进行前端测试。

您可能需要一个仪器化的测试用例。请尝试使用JUnit测试规则来实例化 Activity。考虑到您需要设备或模拟器来运行此测试。


推荐阅读