junit-assert


Junit为我们提供了一些辅助函数,他们用来帮助我们确定被测试的方法是否按照预期的效果正常工作。

单元测试 – Assert

1、Assert.assertEquals(1423263017788772352L, queryUserResp.getUcid());}

报错信息:

Ambiguous method call: both ‘Assert.assertEquals(Object, Object)’ and ‘Assert.assertEquals(long, long)’ match

这是因为我们的queryUserResp.getUcid() 返回Long,而不是long。因此编译器很困惑:它应该将两个参数都转换为 Object,还是应该只将 Long 转换为 long?

2、Json表达式验证器

JsonPathResultMatchers jsonPath(String expression, Object ... args)/ResultMatcher jsonPath(String expression, Matcher matcher)

3、对Map返回值的校验

@Test
public void insertSupplierBrandStrategyValid() throws Exception {

	Map<String, List<String>> map = new HashMap<>();
	List<String> sessionList = new ArrayList<>(2);
	String session1 = UUID.randomUUID().toString();
	String session2 = UUID.randomUUID().toString();
	sessionList.add(session1);
	sessionList.add(session2);
	map.put("1", sessionList);
	when(service.getUserSessions(1)).thenReturn(map);
	MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
			.get("/manager/user/1")
			.accept(MediaType.APPLICATION_JSON)
			.contentType(MediaType.APPLICATION_JSON);
	MvcResult mvcResult = mockMvc.perform(requestBuilder)
			.andDo(print()) //打印输出发出请求的详细信息
			.andExpect(status().isOk())
			.andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasKey("1")))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data", Matchers.hasEntry("1",sessionList )))
			.andReturn();

	System.out.println(mvcResult.getResponse().getContentAsString());
}

4、对List返回值的校验

@Test
public void testQueryCityAreaTree() throws Exception {
	List<CityAreaGroupNode> cityAreaTreeList = new ArrayList<>();
	List<CityAreaGroupNode> cityAreaChildList = new ArrayList<>();

	CityAreaGroupNode cityAreaChild1 = new CityAreaGroupNode();
	cityAreaChild1.setId(1045);
	cityAreaChild1.setTitle(null);
	cityAreaChild1.setName("北京陈冲02");
	cityAreaChild1.setType("area");
	cityAreaChild1.setParent("北京");
	cityAreaChild1.setParentId(null);
	cityAreaChild1.setCity("北京");
	cityAreaChild1.setBusiness("个卖");
	cityAreaChild1.setChildren(null);

	CityAreaGroupNode cityAreaChild2 = new CityAreaGroupNode();
	cityAreaChild2.setId(1035);
	cityAreaChild2.setTitle(null);
	cityAreaChild2.setName("北京刘玉广01");
	cityAreaChild2.setType("area");
	cityAreaChild2.setParent("北京");
	cityAreaChild2.setParentId(null);
	cityAreaChild2.setCity("北京");
	cityAreaChild2.setBusiness("个卖");
	cityAreaChild2.setChildren(null);

	cityAreaChildList.add(cityAreaChild1);
	cityAreaChildList.add(cityAreaChild2);

	CityAreaGroupNode cityAreaGroupNode1 = new CityAreaGroupNode();
	cityAreaGroupNode1.setId(1);
	cityAreaGroupNode1.setTitle(null);
	cityAreaGroupNode1.setType("city");
	cityAreaGroupNode1.setParent(null);
	cityAreaGroupNode1.setName("北京");
	cityAreaGroupNode1.setParentId(null);
	cityAreaGroupNode1.setBusiness(null);
	cityAreaGroupNode1.setChildren(cityAreaChildList);

	cityAreaTreeList.add(cityAreaGroupNode1);

	Mockito.when(auctionParnerService.queryCityAreaTree()).thenReturn(cityAreaTreeList);

	MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders
			.get("/auction/areas/cityAreaTree")
			.param("aid", "966926828067360769");

	mockMvc.perform(requestBuilder)
			.andDo(print()) //打印输出发出请求的详细信息
			.andExpect(status().isOk())
			.andExpect(MockMvcResultMatchers.jsonPath("$.data").isNotEmpty())
			.andExpect(MockMvcResultMatchers.jsonPath("$.data.length()").value(1))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].id").value("1"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].type").value("city"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].name").value("北京"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children.length()").value(2))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[0].id").value(1045))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[0].name").value("北京陈冲02"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[0].type").value("area"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[0].parent").value("北京"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[0].business").value("个卖"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[0].children").isEmpty())
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[1].id").value(1035))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[1].name").value("北京刘玉广01"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[1].type").value("area"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[1].parent").value("北京"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[1].business").value("个卖"))
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[1].title").isEmpty())
			.andExpect(MockMvcResultMatchers.jsonPath("$.data[0].children[1].parentId").isEmpty())
			.andReturn();
}

文章作者: WangQingLei
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 WangQingLei !
  目录