在接口测试过程中,经常需要测试查询接口,若查询条件较多,查询参数组合就会很多,若手工去穷举所有查询组合,一定会费时费力,用下面的排列组合工具类Combination可以很好地解决这个问题,引入组件依赖:
<dependency> <groupId>org.raistlic.lib</groupId> <artifactId>commons-core</artifactId> <version>1.4</version> </dependency> 例如,求数组数组{1,2,3,4,5}的所有组合 @Test public void test2() throws Exception { System.out.println("===== 打印组合:"); List<Integer> asList = Arrays.asList(1, 2, 3, 4, 5); int combinationCount = 0; for (int i = 1; i <= asList.size(); i++) { Combination<Integer> combination = Combination.of(asList, i); combinationCount += combination.getCombinationCount().intValue(); for (List<Integer> list : combination) { System.out.println(list); } } System.out.println("===== 打印组合总数:"); System.out.println("C_5^1+C_5^2+C_5^3+C_5^4++C_5^5="+combinationCount); } 打印结果如下:===== 打印组合: [1] [2] [3] [4] [5] [1, 2] [1, 3] [1, 4] [1, 5] [2, 3] [2, 4] [2, 5] [3, 4] [3, 5] [4, 5] [1, 2, 3] [1, 2, 4] [1, 2, 5] [1, 3, 4] [1, 3, 5] [1, 4, 5] [2, 3, 4] [2, 3, 5] [2, 4, 5] [3, 4, 5] [1, 2, 3, 4] [1, 2, 3, 5] [1, 2, 4, 5] [1, 3, 4, 5] [2, 3, 4, 5] [1, 2, 3, 4, 5] ===== 打印组合总数: C_5^1+C_5^2+C_5^3+C_5^4++C_5^5=31
由此想到用这个工具类来组合我们的查询接口参数,当然我们可以排除无用组合:
@Test @Description(description = "各组合条件查询,全覆盖") public void testGetBargainCount_4() throws Exception { ConvertUtils.register(new DateLocaleConverter(Locale.CHINA,"yyyy-MM-dd HH:mm:ss"),Date.class); Map<String, String> queryParmGroup = buildQueryParmGroup(); Set<Map.Entry<String, String>> entrySet = queryParmGroup.entrySet(); HashSet<BargainQueryParm> failParms = new HashSet<BargainQueryParm>(); for (int i = 0; i < entrySet.size(); i++) { for (List<Map.Entry<String, String>> list : Combination.of(entrySet, i)) { BargainQueryParm queryParam = new BargainQueryParm(); for (Map.Entry<String, String> entry : list) { BeanUtils.setProperty(queryParam,entry.getKey(),entry.getValue()); } Result<Integer> getBargainCount = this.invoke(bargainReadService, "getBargainCount", Integer.class, clientInfo, queryParam); Assert.assertTrue(getBargainCount.isSuccess()); if (getBargainCount.getResult().intValue() == 0) { failParms.add(queryParam); } } } Assert.assertTrue(CollectionUtils.isEmpty(failParms)); } private static Map<String, String> buildQueryParmGroup() { HashMap<String, String> map = new HashMap<String, String>(); map.put("id", "1003266"); map.put("type", "1"); map.put("venderId", "20032"); map.put("businessId", "100073920"); map.put("title", "砍价活动api测试"); map.put("skuId", "1200039810"); map.put("oneCategory", "1315"); map.put("twoCategory", "1342"); map.put("threeCategory", "9733"); map.put("beginTime", "2017-01-04 16:19:45"); map.put("endTime", "2017-02-03 16:19:45"); map.put("status", "1"); map.put("pin", "test_sop01"); map.put("customerPin", "sop_order_new"); return map; } 该开源组件封装了很多实用的算法,具体可以项目源码和api文档 源码地址:https://github.com/raistlic/raistlic-lib-commons-coreapi文档:http://raistlic.github.io/raistlic-lib-commons-core/
