daoTest.ftl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <#include "../function.ftl">
  2. <#include "../variable.ftl">
  3. <#assign pkVar=convertUnderLine(pk) >
  4. <#if isBaseModule = 'true'>
  5. package com.${cAlias}.${cPlatform}.${sys}.persistence.dao;
  6. <#else>
  7. package com.${cAlias}.${cPlatform}.${sys}.${module}.persistence.dao;
  8. </#if>
  9. import javax.annotation.Resource;
  10. import org.junit.Assert;
  11. import org.junit.Test;
  12. import org.springframework.test.annotation.Rollback;
  13. <#if isBaseModule = 'true'>
  14. import com.${cAlias}.${cPlatform}.${sys}.persistence.dao.${class}Dao;
  15. import com.${cAlias}.${cPlatform}.${sys}.persistence.dao.${class}QueryDao;
  16. import com.${cAlias}.${cPlatform}.${sys}.persistence.entity.${po};
  17. <#else>
  18. import com.${cAlias}.${cPlatform}.${sys}.${module}.persistence.dao.${class}Dao;
  19. import com.${cAlias}.${cPlatform}.${sys}.${module}.persistence.dao.${class}QueryDao;
  20. import com.${cAlias}.${cPlatform}.${sys}.${module}.persistence.entity.${po};
  21. </#if>
  22. import com.${cAlias}.${cPlatform}.${sys}.${baseClass}BaseTest;
  23. /**
  24. * ${model.tabComment} dao单元测试类
  25. *
  26. *<pre>
  27. <#if vars.company?exists>
  28. * 开发公司:${vars.company}
  29. </#if>
  30. <#if vars.developer?exists>
  31. * 开发人员:${vars.developer}
  32. </#if>
  33. <#if vars.email?exists>
  34. * 邮箱地址:${vars.email}
  35. </#if>
  36. * 创建时间:${date?string("yyyy-MM-dd HH:mm:ss")}
  37. *</pre>
  38. */
  39. public class ${class}DaoTest extends ${baseClass}BaseTest{
  40. @Resource
  41. private ${class}Dao ${classVar}Dao;
  42. @Resource
  43. private ${class}QueryDao ${classVar}QueryDao;
  44. @Test
  45. @Rollback(true)
  46. public void testCrud(){
  47. ${po} ${poVar}=new ${po}();
  48. <#list model.columnList as col>
  49. <#assign columnName=convertUnderLine(col.columnName)>
  50. <#if col.isPK>
  51. ${poVar}.setId(idGenerator.getId());
  52. <#else>
  53. <#if col.isNotNull>
  54. <#if col.colType="java.util.Date">
  55. ${poVar}.set${columnName?cap_first}(new Date());
  56. <#elseif col.colType="Float">
  57. Integer randId=new Double(100000*Math.random()).intValue();
  58. ${poVar}.set${columnName?cap_first}(Float.parseFloat(randId+""));
  59. <#elseif col.colType="Short">
  60. ${poVar}.set${columnName?cap_first}(new Short("1"));
  61. <#elseif col.colType="Integer">
  62. Integer randId=new Double(100000*Math.random()).intValue();
  63. ${poVar}.set${columnName?cap_first}(randId);
  64. <#elseif col.colType="Long">
  65. Integer randId=new Double(100000*Math.random()).intValue();
  66. ${poVar}.set${columnName?cap_first}(Long.parseLong(randId+""));
  67. <#elseif col.colType="String">
  68. Integer randId=new Double(100000*Math.random()).intValue();
  69. ${poVar}.set${columnName?cap_first}("${poVar}" + randId);
  70. </#if>
  71. </#if>
  72. </#if>
  73. </#list>
  74. //创建一实体
  75. ${classVar}Dao.create(${poVar});
  76. Assert.assertNotNull(${poVar}.getId());
  77. logger.debug("${poVar}1:"+ ${poVar}.getId());
  78. //获取一实体
  79. ${po} ${poVar}2=${classVar}QueryDao.get(${poVar}.getId());
  80. Assert.assertNotNull(${poVar}2);
  81. logger.debug("${poVar}2:" + ${poVar}2.toString());
  82. <#list model.columnList as col>
  83. <#assign columnName=convertUnderLine(col.columnName)>
  84. <#if !col.isPK>
  85. <#if col.isNotNull>
  86. <#if col.colType="java.util.Date">
  87. ${poVar}2.set${columnName?cap_first}(new Date());
  88. <#elseif col.colType="Float">
  89. Integer randId2=new Double(100000*Math.random()).intValue();
  90. ${poVar}2.set${columnName?cap_first}(Float.parseFloat(randId2+""));
  91. <#elseif col.colType="Short">
  92. ${poVar}2.set${columnName?cap_first}(new Short("1"));
  93. <#elseif col.colType="Integer">
  94. Integer randId2=new Double(100000*Math.random()).intValue();
  95. ${poVar}2.set${columnName?cap_first}(randId2);
  96. <#elseif col.colType="Long">
  97. Integer randId2=new Double(100000*Math.random()).intValue();
  98. ${poVar}2.set${columnName?cap_first}(Long.parseLong(randId2+""));
  99. <#elseif col.colType="String">
  100. Integer randId2=new Double(100000*Math.random()).intValue();
  101. ${poVar}2.set${columnName?cap_first}("${poVar}" + randId2);
  102. </#if>
  103. </#if>
  104. </#if>
  105. </#list>
  106. //更新一实体
  107. ${classVar}Dao.update(${poVar}2);
  108. ${po} ${poVar}3=${classVar}QueryDao.get(${poVar}.getId());
  109. Assert.assertNotNull(${poVar}3);
  110. logger.debug("${poVar}3:"+${poVar}3.toString());
  111. //删除一实体
  112. //${classVar}Dao.remove(${poVar}.getId());
  113. }
  114. }