25. 代码实例-单元测试
(一)数据库层使用集成测试
1 29 /** 30 * 测试订单任务数据库访问 31 * @author guanpanpan 32 * 33 */ 34 @RunWith(SpringJUnit4ClassRunner.class) 35 @ContextConfiguration(locations = { "classpath:spring/spring-config.xml" }) 36 public class TestOrderTaskDaoMysql { 37 private IDatabaseTester db; 38 @Resource 39 private OrderTaskDaoMysql orderTaskDaoMysql; 40 41 @Before 42 public void setup() throws Exception { 43 //使用xml文件初始化数据 44 db = DbTestUtil.getLocalMysqlDb1(); 45 IDataSet dataSet1 = new FlatXmlDataSet(new FileInputStream(new File( 46 "src/test/resources/data/mysql/TestOrderTaskMysql_dataset.xml"))); 47 db.setDataSet(dataSet1); 48 db.onSetup(); 49 } 50 51 @Test 52 public void saveOrderTask() throws Exception { 53 OrderTask orderTask = new OrderTask(); 54 orderTask.setOrderId(328514705); 55 orderTask.setAmount(26371); 56 orderTask.setPin("a"); 57 orderTask.setRfType(1); 58 orderTask.setUuId("XD_1_328514705"); 59 orderTask.setStatus(0); 60 orderTask.setStatus2(0); 61 orderTask.setCreated(new Date()); 62 orderTask.setModified(new Date()); 63 orderTask.setCompleteDate(new Date()); 64 orderTaskDaoMysql.saveOrderTask(orderTask); 65 ITable testTable; 66 testTable = db.getConnection().createQueryTable("any_name", 67 "select OrderId from orderTask where orderId=328514705 and pin='a';"); 68 assertEquals(1, testTable.getRowCount()); 69 70 } 71 72 @Test 73 public void clear() throws Exception { 74 75 ITable testTable; 76 testTable = db.getConnection().createQueryTable("any_name", "select OrderId from orderTask;"); 77 assertEquals(2, testTable.getRowCount()); 78 orderTaskDaoMysql.clear(); 79 testTable = db.getConnection().createQueryTable("any_name", "select OrderId from orderTask;"); 80 assertEquals(0, testTable.getRowCount()); 81 82 } 83 84 @Test 85 public void getOrderTaskByUuid() throws Exception { 86 OrderTask orderTask = orderTaskDaoMysql.getOrderTaskByUuid("panpantest", "XD_1_328514700"); 87 assertEquals(263, orderTask.getAmount()); 88 } 89 90 @Test 91 public void findOrderTasksByLevStatus() throws Exception { 92 List<OrderTask> orderTasks = orderTaskDaoMysql.findOrderTasksByLevStatus(1, 2, 3, false); 93 Assert.assertEquals(2, orderTasks.size()); 94 Assert.assertEquals(263, orderTasks.get(0).getAmount()); 95 } 96 97 @Test 98 public void findOrderTasksByLevStatusOrder() throws Exception { 99 List<OrderTask> orderTasks = orderTaskDaoMysql.findOrderTasksByLevStatus(1, 2, 3, true); 100 Assert.assertEquals(2, orderTasks.size()); 101 Assert.assertEquals(1, orderTasks.get(0).getId()); 102 } 103 104 @Test 105 public void findOrderTasksByConsumeStatus() throws Exception { 106 List<OrderTask> orderTasks = orderTaskDaoMysql.findOrderTasksByConsumeStatus(1, 1, 3, false); 107 Assert.assertEquals(2, orderTasks.size()); 108 } 109 110 @Test 111 public void findOrderTasksByConsumeStatusOrder() throws Exception { 112 List<OrderTask> orderTasks = orderTaskDaoMysql.findOrderTasksByConsumeStatus(1, 1, 3, true); 113 Assert.assertEquals(2, orderTasks.size()); 114 Assert.assertEquals(1, orderTasks.get(0).getId()); 115 } 116 117 @Test 118 public void updateCusumeStatus() throws Exception { 119 ITable testTable; 120 testTable = db.getConnection().createQueryTable("any_name", 121 "select 1 from orderTask where uuid='XD_1_328514700' and status=1;"); 122 assertEquals(1, testTable.getRowCount()); 123 long id = orderTaskDaoMysql.getOrderTaskByUuid(1, "XD_1_328514700").getId();//id为自动增加,不知道id,只能得一下 124 int result = orderTaskDaoMysql.updateStatus(1, id, 0, 1); 125 Assert.assertEquals(1, result); 126 testTable = db.getConnection().createQueryTable("any_name", 127 "select 1 from orderTask where uuid='XD_1_328514700' and status=0;"); 128 assertEquals(1, testTable.getRowCount()); 129 result = orderTaskDaoMysql.updateStatus(1, id, 0, 1); 130 Assert.assertEquals(0, result); 131 } 132 133 @Test 134 public void updateCusumeStatusAll() throws Exception { 135 ITable testTable; 136 testTable = db.getConnection().createQueryTable("any_name", 137 "select 1 from orderTask where uuid='XD_1_328514700' and status=1;"); 138 assertEquals(1, testTable.getRowCount()); 139 long id = orderTaskDaoMysql.getOrderTaskByUuid(1, "XD_1_328514700").getId();//id为自动增加,不知道id,只能得一下 140 int result = orderTaskDaoMysql.updateStatus(1, 0, 1); 141 Assert.assertEquals(2, result); 142 testTable = db.getConnection().createQueryTable("any_name", 143 "select 1 from orderTask where uuid='XD_1_328514700' and status=0;"); 144 assertEquals(1, testTable.getRowCount()); 145 result = orderTaskDaoMysql.updateStatus(1, 0, 1); 146 Assert.assertEquals(0, result); 147 } 148 149 @Test 150 public void updateCusumeLevStatus() throws Exception { 151 ITable testTable; 152 testTable = db.getConnection().createQueryTable("any_name", 153 "select 1 from orderTask where uuid='XD_1_328514700' and status2=2;"); 154 assertEquals(1, testTable.getRowCount()); 155 long id = orderTaskDaoMysql.getOrderTaskByUuid(1, "XD_1_328514700").getId();//id为自动增加,不知道id,只能得一下 156 int result = orderTaskDaoMysql.updateGradeStatus(1, id, 0, 2); 157 Assert.assertEquals(1, result); 158 testTable = db.getConnection().createQueryTable("any_name", 159 "select 1 from orderTask where uuid='XD_1_328514700' and status2=0;"); 160 assertEquals(1, testTable.getRowCount()); 161 result = orderTaskDaoMysql.updateGradeStatus(1, id, 0, 2); 162 Assert.assertEquals(0, result); 163 } 164 165 @Test 166 public void updateCusumeLevStatusAll() throws Exception { 167 ITable testTable; 168 testTable = db.getConnection().createQueryTable("any_name", 169 "select 1 from orderTask where uuid='XD_1_328514700' and status2=2;"); 170 assertEquals(1, testTable.getRowCount()); 171 long id = orderTaskDaoMysql.getOrderTaskByUuid(1, "XD_1_328514700").getId();//id为自动增加,不知道id,只能得一下 172 int result = orderTaskDaoMysql.updateGradeStatus(1, 0, 2); 173 Assert.assertEquals(2, result); 174 testTable = db.getConnection().createQueryTable("any_name", 175 "select 1 from orderTask where uuid='XD_1_328514700' and status2=0;"); 176 assertEquals(1, testTable.getRowCount()); 177 result = orderTaskDaoMysql.updateGradeStatus(1, 0, 2); 178 Assert.assertEquals(0, result); 179 } 180 181 @Test 182 public void countOrderTasksByConsumeStatus() throws Exception { 183 assertEquals(2, orderTaskDaoMysql.countOrderTasksByConsumeStatus(1, 1)); 184 } 185 186 @Test 187 public void countOrderTasksByLevStatus() throws Exception { 188 assertEquals(2, orderTaskDaoMysql.countOrderTasksByLevStatus(1, 2)); 189 } 190 191 @Test 192 public void findNewOrderTasks() throws Exception { 193 List<OrderTask> orderTasks = orderTaskDaoMysql.findNewOrderTasks(1, 100); 194 assertEquals(2, orderTasks.size()); 195 assertEquals(2, orderTasks.get(0).getId()); 196 orderTasks = orderTaskDaoMysql.findNewOrderTasks(1, 1); 197 assertEquals(1, orderTasks.size()); 198 assertEquals(2, orderTasks.get(0).getId()); 199 assertEquals("2009-02-04 13:54:00", DateUtil.getDateStr(orderTasks.get(0).getCompleteDate())); 200 } 201 202 @After 203 public void tearDown() throws Exception { 204 db.setTearDownOperation(DatabaseOperation.DELETE_ALL); 205 db.onTearDown(); 206 } 207 }
1 <dataset> 2 <orderTask id="1" OrderId="328514700" Amount="263.719" Pin="panpantest" RfType="1" UuId="XD_1_328514700" status="1" status2="2" 3 completeDate="2009-01-04 13:54:00.000" created="2009-01-04 13:54:00.000" modified="2009-01-04 13:54:00.000" /> 4 <orderTask id="2" OrderId="328514701" Amount="263.719" Pin="panpantest1" RfType="1" UuId="XD_1_328514701" status="1" status2="2" 5 completeDate="2009-02-04 13:54:00.000" created="2013-01-04 13:54:00.000" modified="2013-01-04 13:54:00.000" /> 6 </dataset>
(二)核心逻辑要有单元测试
1 21 /** 22 * 加消费额度锁定单元测试 23 * @author guanpanpan 24 * 25 */ 26 @RunWith(SpringJUnit4ClassRunner.class) 27 @ContextConfiguration(locations = { "classpath:spring/spring-config.xml" }) 28 public class TestAddConsumeLock { 29 @Resource 30 private DataLock addConsumeLock; 31 private OrderTaskDaoMysql orderTaskDaoMysql1; 32 private OrderTaskDaoMysql orderTaskDaoMysql2; 33 private OrderTaskDaoMysql orderTaskDaoMysqlOld;//避免影响其它测试 34 35 @Before 36 public void setup() throws Exception { 37 //mock rep 38 List<OrderTask> orderTasks = new ArrayList<OrderTask>(); 39 orderTasks.add(new OrderTask(1, "1")); 40 orderTasks.add(new OrderTask(2, "2")); 41 orderTasks.add(new OrderTask(3, "3")); 42 orderTasks.add(new OrderTask(4, "4")); 43 orderTaskDaoMysql1 = EasyMock.createMock(OrderTaskDaoMysql.class); 44 EasyMock.expect(orderTaskDaoMysql1.findOrderTasksByConsumeStatus(1, OrderTask.SYN_WAIT, 4, false)).andReturn(orderTasks).times(1); 45 EasyMock.expect(orderTaskDaoMysql1.updateStatus(1, 1, OrderTask.SYN_LOCK, OrderTask.SYN_WAIT)).andReturn(1).times(1); 46 EasyMock.expect(orderTaskDaoMysql1.updateStatus(1, 2, OrderTask.SYN_LOCK, OrderTask.SYN_WAIT)).andReturn(0).times(1); 47 EasyMock.expect(orderTaskDaoMysql1.updateStatus(1, 3, OrderTask.SYN_LOCK, OrderTask.SYN_WAIT)).andReturn(1).times(1); 48 EasyMock.replay(orderTaskDaoMysql1); 49 50 orderTaskDaoMysql2 = EasyMock.createMock(OrderTaskDaoMysql.class); 51 EasyMock.expect(orderTaskDaoMysql2.findOrderTasksByConsumeStatus(1, OrderTask.SYN_WAIT, 4, false)).andReturn(orderTasks).times(1); 52 EasyMock.expect(orderTaskDaoMysql2.updateStatus(1, 1, OrderTask.SYN_LOCK, OrderTask.SYN_WAIT)).andReturn(0).times(1); 53 EasyMock.expect(orderTaskDaoMysql2.updateStatus(1, 2, OrderTask.SYN_LOCK, OrderTask.SYN_WAIT)).andReturn(0).times(1); 54 EasyMock.expect(orderTaskDaoMysql2.updateStatus(1, 3, OrderTask.SYN_LOCK, OrderTask.SYN_WAIT)).andReturn(0).times(1); 55 EasyMock.expect(orderTaskDaoMysql2.updateStatus(1, 4, OrderTask.SYN_LOCK, OrderTask.SYN_WAIT)).andReturn(0).times(1); 56 EasyMock.replay(orderTaskDaoMysql2); 57 58 orderTaskDaoMysqlOld = (OrderTaskDaoMysql) ReflectionTestUtils.getField(addConsumeLock, "orderTaskDaoMysql"); 59 60 } 61 62 /** 63 * 正常锁定测试 64 */ 65 @Test 66 public void lock() throws Exception { 67 ReflectionTestUtils.setField(addConsumeLock, "orderTaskDaoMysql", orderTaskDaoMysql1); 68 List<OrderTask> orderTaskLocks = addConsumeLock.lockDatas(1,1,1); 69 EasyMock.verify(orderTaskDaoMysql1); 70 Assert.assertEquals(2, orderTaskLocks.size()); 71 Assert.assertEquals(1, orderTaskLocks.get(0).getId()); 72 Assert.assertEquals(3, orderTaskLocks.get(1).getId()); 73 } 74 75 /** 76 * 无锁定值测试 77 */ 78 @Test 79 public void lockEmpty() throws Exception { 80 ReflectionTestUtils.setField(addConsumeLock, "orderTaskDaoMysql", orderTaskDaoMysql2); 81 List<OrderTask> orderTasks = addConsumeLock.lockDatas(1,1,1); 82 EasyMock.verify(orderTaskDaoMysql2); 83 Assert.assertEquals(0, orderTasks.size()); 84 } 85 86 @After 87 public void tearDown() throws Exception { 88 ReflectionTestUtils.setField(addConsumeLock, "orderTaskDaoMysql", orderTaskDaoMysqlOld);//避免影响其它测试 89 } 90 }
(三)结合程序入口的集成测试(验收测试)
1 27 28 /** 29 * 加消费额度的验收测试 30 * @author guanpanpan 31 * 32 */ 33 @RunWith(SpringJUnit4ClassRunner.class) 34 @ContextConfiguration(locations = { "classpath:spring/spring-config.xml" }) 35 public class TestAddConsumeJob { 36 @Resource 37 private AddConsumeJob addConsumeJob; 38 private IDatabaseTester mysqlDb1; 39 40 @Before 41 public void setup() throws Exception { 42 mysqlDb1 = DbTestUtil.getLocalMysqlDb1(); 43 IDataSet dataSet1 = new FlatXmlDataSet(new FileInputStream(new File( 44 "src/test/resources/data/TestAddConsumeJob/TestAddConsumeJob_consume_dataset.xml"))); 45 mysqlDb1.setDataSet(dataSet1); 46 mysqlDb1.onSetup(); 47 48 } 49 50 @Test 51 public void dealConsumeChange() throws Exception { 52 ITable testTable; 53 testTable = mysqlDb1.getConnection().createQueryTable("any_name", 54 "select * from orderTask where status=0 and OrderId=328514700;"); 55 assertEquals(1, testTable.getRowCount()); 56 addConsumeJob.start(); 57 addConsumeJob.sleepStopAndWait(); 58 //任务变更 正常 59 testTable = mysqlDb1.getConnection().createQueryTable("any_name", 60 "select modified from orderTask where status=1 and status2=1 and OrderId=328514700;"); 61 assertEquals(1, testTable.getRowCount()); 62 Date modified = (Date) (testTable.getValue(0, "modified")); 63 assertEquals(DateUtil.getDateDayStr(new Date()), DateUtil.getDateDayStr(modified)); 64 //金额<0不同步 65 testTable = mysqlDb1.getConnection().createQueryTable("any_name", 66 "select modified from orderTask where status=11 and OrderId=328514703;"); 67 assertEquals(1, testTable.getRowCount()); 68 modified = (Date) (testTable.getValue(0, "modified")); 69 assertEquals(DateUtil.getDateDayStr(new Date()), DateUtil.getDateDayStr(modified)); 70 //正常消费台帐 71 testTable = mysqlDb1.getConnection().createQueryTable( 72 "any_name", 73 "select completeDate from consumeDetail_42 where OrderId=328514700 and amount=100000001 and pin='koyong';"); 74 assertEquals(1, testTable.getRowCount()); 75 Date completeDate = (Date) (testTable.getValue(0, "completeDate")); 76 assertEquals("2013-01-05", DateUtil.getDateDayStr(completeDate)); 77 //消费总额 78 testTable = mysqlDb1.getConnection().createQueryTable("any_name", 79 "select amount,finishNum as amount from jdConsume_42 where pin='koyong';"); 80 assertEquals(1, testTable.getRowCount()); 81 assertEquals(100000001, ((BigInteger) (testTable.getValue(0, "amount"))).intValue()); 82 assertEquals(1, ((Integer) (testTable.getValue(0, "finishNum"))).intValue()); 83 84 } 85 86 @Test 87 public void dealConsumeChangeRollBack() throws Exception { 88 //测试回滚 89 ConsumeChangeImpl.throwRuntimeExceptionInTransation = true; 90 ITable testTable; 91 testTable = mysqlDb1.getConnection().createQueryTable("any_name", 92 "select * from orderTask where status=0 and OrderId=328514700;"); 93 assertEquals(1, testTable.getRowCount()); 94 addConsumeJob.start(); 95 addConsumeJob.sleepStopAndWait(); 96 //任务变更 正常 97 testTable = mysqlDb1.getConnection().createQueryTable("any_name", 98 "select modified from orderTask where status=1 and OrderId=328514700;"); 99 assertEquals(0, testTable.getRowCount()); 100 //金额<0不同步 101 testTable = mysqlDb1.getConnection().createQueryTable("any_name", 102 "select modified from orderTask where status=11 and OrderId=328514703;"); 103 assertEquals(1, testTable.getRowCount()); 104 //正常消费台帐 105 testTable = mysqlDb1.getConnection().createQueryTable( 106 "any_name", 107 "select completeDate from consumeDetail_42 where OrderId=328514700 and amount=100000001 and pin='koyong';"); 108 assertEquals(0, testTable.getRowCount()); 109 //消费总额 110 testTable = mysqlDb1.getConnection().createQueryTable("any_name", 111 "select amount as amount from jdConsume_42 where pin='koyong' and finishNum=1;"); 112 assertEquals(0, testTable.getRowCount()); 113 ConsumeChangeImpl.throwRuntimeExceptionInTransation = false; 114 115 } 116 117 @After 118 public void tearDown() throws Exception { 119 mysqlDb1.setTearDownOperation(DatabaseOperation.DELETE_ALL); 120 mysqlDb1.onTearDown(); 121 } 122 }
减少重复学习,在原来掌握的基础上进行提高。质疑驱动,不断对已经掌握的知识进行质疑。
如果能把已经掌握的东西总结成模式,才是真正掌握了。
浙公网安备 33010602011771号