java - 断言 - 内部调用问题
问题描述
大家好,我的第一个测试有一些问题
我正在写这个片段,但我一直遇到这个问题:
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!");
}
}
});
}
解决方案
断言检查内部值。如果不满足断言,它们会抛出异常。因此,您可以检查完整的堆栈跟踪。
该异常可能发生在该行中Assert.assertNotNull(tester.cameraSource);
。这是因为 cameraSource 将为空。这是因为您只创建了一个类的实例,但没有onCreate
调用类似的事件。一个简单的单元测试不足以进行前端测试。
您可能需要一个仪器化的测试用例。请尝试使用JUnit测试规则来实例化 Activity。考虑到您需要设备或模拟器来运行此测试。
推荐阅读
- angular - 我的 Angular 组件没有加载到 Office Word Addin 操作窗格中,尽管它在办公环境之外运行良好
- php - 在 php 或 javascript 中列出带有日期名称的每个日期
- node.js - Sequelize - 包括所有关联过滤结果
- c++ - 我想使用 Nodemcu 和 blynk 连接两个 yf-s201 水流传感器,一个作为流入,另一个作为流出
- java - URI在线判断1435 Square Matrix I (Presentation Error)
- python-3.x - 如何使用python重命名同一文件夹但不同子文件夹中不同格式的文件
- symfony - Symfony 5 自定义 404 页面
- casting - 是否有任何编程语言可以禁止返回类型的类转换?
- r - 将 Data Wrangling 工作流合并到一个函数中
- notepad++ - 最小化启动时运行记事本++