第七次作业——团队作业——项目测试
1、项目的测试工作安排
测试工作基本由编程人员在编码的同时进行,对于较复杂的业务逻辑进行测试。
2、测试工具与使用
测试工具: Unitils
Unitils测试框架构建在DbUnit与EasyMock项目之上并与TestNg和TestNG相结合。支持数据库测试,支持利用Mock对象进行测试并提供与Spring和Hibernate相集成。
Unitils 使用
1.引入相关 jar 包
<!-- 依赖的测试类库 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test-mvc</artifactId>
<version>1.0.0.M1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito-all.version}</version>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-spring</artifactId>
<version>${unitils.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-testng</artifactId>
<version>${unitils.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>${dbunit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.unitils</groupId>
<artifactId>unitils-dbunit</artifactId>
<version>${unitils.version}</version>
<scope>test</scope>
</dependency>
2.unitils 配置
unitils.properties
unitils.modules=database,dbunit,hibernate,spring
#自定义扩展模块
#unitils.module.dbunit.className=org.unitils.dbunit.DbUnitModule
#配置数据库连接
database.driverClassName=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/oc?useUnicode=true&characterEncoding=UTF-8
database.dialect = mysql
database.userName=root
database.password=root
#配置数据库维护策略
# The database maintainer is disabled by default.
updateDataBaseSchema.enabled=true
#配置数据库创建策略
#This table is by default not created automatically
dbMaintainer.autoCreateExecutedScriptsTable=false
#dbMaintainer.script.locations=D:/actionSpring/chapter/chapter8/src/test/resources/dbscripts
#数据集加载策略
DbUnitModule.DataSet.loadStrategy.default=org.unitils.dbunit.datasetloadstrategy.impl.InsertLoadStrategy(此处配置加载策略 InsertLoadStrategy:只插入数据)
#配置数据集工厂
#DbUnitModule.DataSet.factory.default=org.unitils.dbunit.datasetfactory.impl.MultiSchemaXmlDataSetFactory
#DbUnitModule.ExpectedDataSet.factory.default=org.unitils.dbunit.datasetfactory.impl.MultiSchemaXmlDataSetFactory
DbUnitModule.DataSet.factory.default=com.joc.test.dataset.excel.MultiSchemaXlsDataSetFactory
DbUnitModule.ExpectedDataSet.factory.default=com.joc.test.dataset.excel.MultiSchemaXlsDataSetFactory
DatabaseModule.Transactional.value.default=commit
# XSD generator
dataSetStructureGenerator.xsd.dirName=resources/xsd
dbMaintainer.generateDataSetStructure.enabled=true
3.测试 TeacherDao
基类
package com.joc.dao;
import org.unitils.UnitilsTestNG;
import org.unitils.spring.annotation.SpringApplicationContext;
@SpringApplicationContext( {"classpath:/applicationContext.xml" }) //获取资源
public class BaseDaoTest extends UnitilsTestNG{
}
4.TeacherDaoTest 类
package com.joc.dao;
import com.joc.domain.Teacher;
import org.testng.annotations.Test;
import org.unitils.spring.annotation.SpringBean;
import static junit.framework.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
public class TeacherDaoTest extends BaseDaoTest {
@SpringBean("teacherDao") //注解获得bean
private TeacherDao teacherDao;
@Test
public void findUserByUserName() {
Teacher teacher = teacherDao.getUserByUserName("test");
assertNull(teacher,"不存在用户名为test的用户!");
teacher = teacherDao.getUserByUserName("12345");
assertNotNull(teacher,"12345用户存在!");
assertEquals("12345", teacher.getTeacherUserName());
assertEquals("12345",teacher.getTeacherPassword());
}
}
结果
5.登录测试(有bug未改)
package com.joc.web;
import com.joc.web.controller.BaseController;
import org.springframework.test.web.server.MockMvc;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.springframework.test.web.server.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.server.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.server.setup.MockMvcBuilders.xmlConfigSetup;
public class LoginControllerTest extends BaseController {
private MockMvc mockMvc;
String appContextPath = "classpath:/applicationContext.xml";
String webContextPath = "file:E:/IDEAProject/AutoCS/src/main/webapp/WEB-INF/web-servlet.xml";
String warDir = "src/main/webapp";
@BeforeClass
public void setUp() {
mockMvc = xmlConfigSetup(appContextPath, webContextPath)
.configureWebAppRootDir(warDir, false)
.build();
}
@Test
public void testDoLogin() throws Exception {
mockMvc.perform(post("/login").param("userName", "tom").param("password", "12345"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/index.jsp"));
mockMvc.perform(post("/login").param("userName", "123456").param("password", "12345"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/index.jsp"));
mockMvc.perform(post("/login").param("userName", "12345").param("password", "12345"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/jsp/success1.jsp"));
}
@Test
public void testLogout() throws Exception {
mockMvc.perform(get("/login/doLogout"))
.andExpect(status().isOk())
.andExpect(redirectedUrl("/index.jsp"));
}
}
6.手动测试,启动Tomcat服务器,测试成功
3、测试用例文档
登录测试
4、测试体会
unitils 测试功能十分强大,Dao、Service、Web层都可以进行测试,只要配置几个xml文件,获取资源时尽量不要采用xml配置,尽量采用注解方法。但是强大也会有困扰,由于方法多种多样,有时候采用多种测试方法反而会出现各种bug,所以尽量采用某一种或几种方法测试。当然,在团队合作当中,并不要求每个人都充分掌握测试技巧,所以要分好工,有时候知道怎么用就行了。
5、项目测试评述
登录功能及Dao、Service、Web测试并没有任何错误,整体功能还行。但是用户登录后点击首页localhost地址会改变,但是页面内容并没有任何变化,且原有的某些字端会消失。这个需要改善。