如何对Android应用程序进行JUnit测试

2025-03-21 00:14:51
推荐回答(1个)
回答1:

被测试类代码:
package com.junit.test;
public class CalService {
public int add(int x, int y) {
return x + y;
}
}
测试类代码:
import android.app.AlertDialog;
import com.junit.test.CalService;
import junit.framework.TestCase;
public class testCalService extends TestCase {
public void testAdd() throws Exception{
CalService cal = new CalService();
int result = cal.add(2, 3);
assertEquals(5, result);
}
}
写测试类时候需要注意两点:
1. 要想使用JUnit测试,需要将测试类继承TestCase
2. 测试方法需要捕获异常