java--单测
来源:互联网 发布:4518无法网络打印 编辑:程序博客网 时间:2024/05/16 08:54
1.Mapper层(DAO层)单测
- 该mapper层主要用于测试编写的SQL是否存在问题,测试数据不经过数据库,不会对现存的表数据造成影响。
- 优点:测试数据不走本地数据库的表,不会对表中的数据造成影响。
- 缺点:测试配置的文件比较繁琐,需要花费一定的时间成本。
本例使用的是在XxxMapper.xml中写SQL,开发工具为IDE,maven工程(以comment为例)。
- 目录结构
- schema-init.sql
说明:该文件中存放的是要测试的数据库表结构(待会测试走的就是这个表,而不是我们当前使用的mysql或者Oracle里面的表)
- comment-delete-setupData.xml
说明:随便造数据即可,但日期类型需要特别注意,其他类型默认赋值为字符串类型;其他xxx.xml类似 - 测试类(重点)
说明:commentMapperTest.java全部代码如下,我们会注意到有setUpData和expectedData之分,前者是我们操作的数据,后者是我们操作之后希望变成的数据,通过这样的判断就可以知道SQL是否有错
import com.github.springtestdbunit.annotation.DatabaseSetup;import com.github.springtestdbunit.annotation.ExpectedDatabase;import com.github.springtestdbunit.assertion.DatabaseAssertionMode;import org.junit.Assert;import org.junit.Test;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import java.util.List; public class CommentMapperTest extends BaseOrmTest { protected static Logger logger = LoggerFactory.getLogger(CommentMapperTest.class); @Autowired private CommentMapper commentMapper; @Test @ExpectedDatabase(table = "sns_comment", assertionMode = DatabaseAssertionMode.NON_STRICT, value = "/data/comment/comment-insert-expectedData.xml") public void testInsert() { CommentEntity commentEntity = new CommentEntity(); commentEntity.setCommentId("1"); commentEntity.setParentId("1"); commentEntity.setMeterialId("1"); commentEntity.setContent("1"); commentEntity.setCreater("1"); commentEntity.setReply("1"); commentEntity.setReplyer("1"); commentEntity.setUsefulTimes(1); commentEntity.setUselessTimes(1); Assert.assertEquals(1, commentMapper.insert(commentEntity)); } @Test @DatabaseSetup(value = "/data/comment/comment-update-setupData.xml") @ExpectedDatabase(table = "sns_comment", assertionMode = DatabaseAssertionMode.NON_STRICT, value = "/data/comment/comment-update-expectedData.xml") public void testUpdate() { CommentEntity commentEntity = new CommentEntity(); commentEntity.setCommentId("1"); commentEntity.setUselessTimes(2); Assert.assertEquals(1, commentMapper.update(commentEntity)); } @Test @DatabaseSetup(value = "/data/comment/comment-queryById-setupData.xml") public void testQueryById() { CommentEntity commentEntity = commentMapper.queryById("1"); print(commentEntity); Assert.assertNotNull(commentEntity); Assert.assertEquals("1", commentEntity.getCreater()); } @Test @DatabaseSetup(value = "/data/comment/comment-query-setupData.xml") public void testQuery() { CommentQueryBean queryBean = new CommentQueryBean(); queryBean.setCreater("1"); List<CommentEntity> commentEntityList = commentMapper.query(queryBean); print(commentEntityList); Assert.assertNotNull(commentEntityList); } @Test @DatabaseSetup(value = "/data/comment/comment-delete-setupData.xml") public void testDelete() { Assert.assertEquals(1, commentMapper.delete("1")); } @Test @DatabaseSetup(value = "/data/comment/comment-queryByPage-setupData.xml") public void testSelectByPage() { CommentQueryBean queryBean = new CommentQueryBean(); queryBean.setCreater("1"); Page<CommentEntity> page = new Page<>(5); page.setPageNo(1); Page<CommentEntity> commentEntityPage = commentMapper.queryByPage(page, queryBean); print(commentEntityPage); Assert.assertNotNull(commentEntityPage); Assert.assertEquals(5, commentEntityPage.getResults().size()); }}
- CommentMapper.java -
@Repository
public interface CommentMapper {int delete(String commentId);int insert(CommentEntity commentEntity);CommentEntity queryById(String commentId);List<CommentEntity> query(CommentQueryBean queryBean);int update(CommentEntity commentEntity);Page<CommentEntity> queryByPage(Page<CommentEntity> page, CommentQueryBean queryBean);}
- CommentMapper.xml
说明:无
配置文件
该项目使用的是spring boot,所以配置文件为bootstrap.yml
- bootstrap.yml中添加
mybatis: typeAliasesPackage: com.xxx.xxx.**.orm.mapper.entity mapperLocations: classpath:com/xxx/xxx/**/orm/mapper/*.xml
- BaseOrmTest.java(重点)
import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.serializer.SerializerFeature;import com.github.springtestdbunit.DbUnitTestExecutionListener;import com.github.springtestdbunit.annotation.DbUnitConfiguration;import org.junit.runner.RunWith;import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.TestExecutionListeners;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;import org.springframework.test.context.support.DirtiesContextTestExecutionListener;//@RunWith(SpringJUnit4ClassRunner.class)//@SpringBootTest(classes={DataSourceAutoConfiguration.class, MybatisAutoConfiguration.class, MyBatisConfig.class})//--如上配置在去掉test下面的application.properties之后,会执行基main下面mysql配置的物理数据库测试@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = {MybatisAutoConfiguration.class, H2Config.class})@TestExecutionListeners({ DbUnitTestExecutionListener.class, DirtiesContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class})@DbUnitConfiguration(databaseConnection = "h2DataSource")//每个方法执行完毕清理痕迹//@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)public class BaseOrmTest { protected static Logger logger = LoggerFactory.getLogger(BaseOrmTest.class); protected void print(Object object) { logger.info(JSON.toJSONString(object, SerializerFeature.PrettyFormat)); }}
补充说明:
CommentEntity:里面的字段是和数据库表中的字段一一对应的。
CommentQueryBean:里面的字段一般和数据库表中字段一致,可能额外有其他特殊字段,比如分页传的页数和每页呈现的记录数等。
CommentVO:是用于呈现给页面的字段,打个比方,数据库表中有是十个字段,我们页面只需要呈现其中的五个,那么CommentVO中需要装的就是这五个字段,有人会疑问,直接用CommentEntity不就好了,但是我的师傅告诉我,这样会把我们数据库表的所以字段暴露出去,不安全,所以,需要什么,就呈现什么。
xxxMapper.xml的SQL:我们的写作模式是,查询出来的字段是数据库表的所有字段,作为查询的条件也是数据库表的所有字段,这样,分别写一个CRUD的SQL就可以照顾到所有的场景,当然,特殊场景另算。
阅读全文
0 0
- java--单测
- java单态
- Java单例注意事项
- java单例模式
- JAVA 初学单词表
- Java单例模式
- java单例模式
- java单例
- java单例模式
- Java单例模式
- Java 单态模式
- 单的JAVA浏览器
- Java单例模式
- java单例模式
- java单例讲解
- java 单例
- Java单例模式
- java 单例模式
- “玲珑杯”ACM比赛 Round #18
- CSS性能优化
- ES(elasticsearch)搜索引擎安装和使用
- Gym-101353H Simple Path(树型dp)
- 朴素贝叶斯分类的Python实现
- java--单测
- linux下如何查看某软件是否已安装
- SGU167 I-country
- POI之Word文档读取-yellowcong
- HDU-5977 Garden of Eden(树分治+枚举子集)
- JS中style.display和style.visibility的区别
- 关于const引用
- Mac Install Raspberry system to machine
- linux ftp 550 Permission denied. ftp Passive mode refused.