一、在项目的AndroidManifest.xml文件中做如下更改:
在application节点下添加
,如图:
在根节点下添加如下内容:
其中targetPackage跟你所在项目的包名相同(参见你的AppManifest.xml文件顶部:
二、为你所在项目添加如下jar包引用
1、junit-4.11
2、harmcrest-core-1.3
3、annotations
三、调整项目对jar包的引用顺序
做这个调整是因为我们使用的Android API中包含了JUnit 3.8,默认SDK是放在最前面的,否则代码中的Junit会被认为是Junit 3.8,那你就只能使用Junit 3的测试方式了。
四、在项目目录树中,创建一个与src平级的目录用于存放所有的测试类。
简单起见我使用的是tests.
除了创建目录,我们还需要指定新创建的目录为测试根目录:
右键tests目录,选择'Mark Directory As'--->'Test Sources Root'
五、在你的测试根目录下创建你的测试分类目录及具体的测试类
在测试类内部你需要的只是给测试方法加上@Test标注。
六、运行你的测试
1、在代码编辑界面,在你的测试方法体中点击右键,在d出的菜单中选中以JUnit的方式运行
这种方式非常适合刚写完测试,亟待看到某个测试的运行结果的场景。
还有一种使用快捷键的方式来单独运行某个测试,我们只需要将文本焦点切换到某个测试方法中,然后在使用组合键Ctrl+Shift+F10就可以达到同上面一样的测试效果。
2、编辑运行Configuration
点击Configuration按钮,在d出界面中按如下步骤创建一个JUnit运行配置
-
1 建立一个test的目录,在此目录下放置所有的JunitTestCase类和TestCase的配置文件2 将项目中的Spring配置文件(默认名称为applicationContext.xml)复制到test目录下,并重新命名为JunitTestConf.xml。
3 根据Junit测试的需要修改JunitTestConf.xml文件中的内容,如数据库连接等。
4 新建一个名为SpringConfForTest.java的类,在此类中配置Spring启动所需的配置文件,并启动Spring。此类的内容如下:
package test
import org.junit.AfterClass
import org.junit.BeforeClass
import org.junit.Test
import org.springframework.context.ApplicationContext
import org.springframework.context.support.ClassPathXmlApplicationContext
import com.soma.global.WebContextHolder
public class SpringConfForTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//Spring启动所需要的配置参数文件,其中test/JunitTestConf.xml文件中保存了数据库连接等参数,可根据具体情况做修改
String[] paths = new String[] {"test/JunitTestConf.xml", "com/soma/conf/applicationContext-dao-hr.xml","com/soma/conf/applicationContext-dao.xml","com/soma/conf/applicationContext-dao-bug.xml","com/soma/conf/applicationContext-dao-change.xml","com/soma/conf/applicationContext-dao-common.xml","com/soma/conf/applicationContext-service-hr.xml" }
//启动Spring,得到Spring环境上下文
ApplicationContext ctx = new ClassPathXmlApplicationContext(paths)
//在此类启动时,将Spring环境上下文保存到单根类WebContextHolder中,以提供给其它的测试类使用
WebContextHolder.getInstence().setApplicationContext(ctx)
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Test
public void test(){
//必须要写一个test空方法,否则SpringConfForTest类不会启动
}
}
5 新建TestSuite类,类名为AllTests,类的内容如下所示:
package test
import junit.framework.Test
import junit.framework.TestSuite
import org.junit.runner.RunWith
import org.junit.runners.Suite
import test.com.soma.domain.busilogic.hr.HrBusiLogicTest
import test.com.soma.domain.service.hr.checkOverTimeDateTest
@RunWith(Suite.class)
@Suite.SuiteClasses({
SpringConfForTest.class,
HrBusiLogicTest.class,
checkOverTimeDateTest.class
})
/**
* 批量执行Junit测试类,把类名写入到上面的Suite.SuiteClasses({})中,用逗号分隔
*/
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test for test")
//$JUnit-BEGIN$
//$JUnit-END$
return suite
}
}
注意:将SpringConfForTest.class放在第一个执行,以启动Spring配置环境,把自己的TestCase类放到后面,用逗号分开。在测试时,只要执行这个TestSuite类就可以了。
6 写自己的TestCase类,以CheckOverTimeDateTest.java为例子,文件内容如下:
public class CheckOverTimeDateTest {
private static HrTbovertimeManager hrTbovertimeManager
private static ExcuteSqlDAO excuteSqlDAO
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//从Spring上下文中得到hrTbovertimeManager接口类的实例
hrTbovertimeManager=(HrTbovertimeManager)BeanUtil.getBean("hrTbovertimeManager")
excuteSqlDAO = (ExcuteSqlDAO) BeanUtil.getBean("excuteSqlDAO")
}
@Test
public void testGetProjectList()throws Exception {
List<OvertimeDetailValue>overtimeDetailValueList = new ArrayList<OvertimeDetailValue>()
int index = 9
for(int i = 1 i <= indexi++){
OvertimeDetailValue overtimeDetailValue = new OvertimeDetailValue()
overtimeDetailValue.setOtApplyDate("2009-05-0"+i)
overtimeDetailValueList.add(overtimeDetailValue)
}
String resultStr = hrTbovertimeManager.checkOverTimeDate(overtimeDetailValueList)
assertEquals("false", resultStr)
}
/**
* 导入2009-03月份出勤记录excel文件,返回null表示导入成功,需要先删除3月份的数据
*/
@Test
public void testSaveExcelDutyInformation() throws Exception{
// 在导入3月份出勤记录前先删除3月份的记录,执行delete from hr_tbdutyinformation
excuteSqlDAO.excuteSql("delete from hr_tbdutyinformation where dutydate>='2009-02-26' and dutydate<='2009-03-25'")
// System.out.println("----------"+System.getProperty("user.dir")+"/src/test/duty200903.xls")
String fileName = System.getProperty("user.dir")
+ "/src/test/duty200903.xls"
assertNull(hrTbdutyInformationManager.saveExcelDutyInformation(fileName))
}
}
说明:BeanUtil.getBean("")相当于WebContextHolder.getInstence().getApplicationContext().getBean(""),只是对此方法做了封装。
7 在Eclipse中,启动AllTests,选择“Run As JunitTest”,即可先启动Spring环境,再依次运行你自己所写的JunitTestCase,是不是很简单哪?赶快动手试试吧。
在工程中右键built path 添加junit4,然后再创一个包命名为test....,里面创建test...... ..类,@beforepublic void init()
@test
public void test1()
大概这样子,要记得实例化,才能调用
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)