java [CashierControllerTest] Spring Controller单元测试类#java #spring
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java [CashierControllerTest] Spring Controller单元测试类#java #spring相关的知识,希望对你有一定的参考价值。
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:spring-mvc.xml")
public class CashierControllerTest {
@Autowired
private AppService appService;
@Autowired
private SecureManager secureManager;
@BeforeClass
public static void setSystemProperty() {
Properties properties = System.getProperties();
properties.setProperty("spring.profiles.active", "test");
}
@Autowired
@Qualifier("webApplicationContext")
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testWxJsTicket() throws Exception {
MvcResult result = this.mockMvc.perform(get("/api/pay/wxJsTokens?appid=6000")
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(status().isOk())
.andReturn();
System.out.println(result.getResponse().getContentAsString());
}
@Test
public void testUnifiedOrder() throws Exception {
Map<String, String> map = new HashMap<>();
map.put("version", "v1.0");
map.put("pageUrl", "");
map.put("bgUrl", "");
map.put("orderId", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime()));
map.put("orderAmount", "0.01");
map.put("orderTime", new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime()));
map.put("productName", "中文测试羊肉串");
map.put("productNum", "1");
map.put("productDesc", "中文测试描述羊肉串");
map.put("appId", "1999");
map.put("accessPlatform", "6");
map.put("channelCode", "ALIPAY");
map.put("signType", "1");
map.put("sign", this.sign(map));
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = post("/api/pay/jsapi")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE);
for (Map.Entry<String, String> entry : map.entrySet()) {
String value = entry.getValue();
if (entry.getKey().equals("productName") || entry.getKey().equals("productDesc")) {
value = URLEncoder.encode(entry.getValue(), "utf-8");
}
mockHttpServletRequestBuilder.param(entry.getKey(), value);
}
MvcResult res = this.mockMvc.perform(mockHttpServletRequestBuilder)
.andExpect(status().isOk())
.andReturn();
System.out.println(res.getResponse().getContentAsString());
System.out.println(JsonPath.<String>read(
res.getResponse().getContentAsString(),
"$.data.orderStr"
));
}
@Test
public void testCashierOrder() throws Exception {
String time = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
String appId = "1999";
Map<String, String> map = new HashMap<>();
map.put("version", "v1.0");
map.put("pageUrl", "");
map.put("bgUrl", "");
map.put("orderId", time);
map.put("orderAmount", "0.01");
map.put("orderTime", time);
map.put("productName", "中文测试羊肉串");
map.put("productNum", "1");
map.put("productDesc", "中文测试描述羊肉串");
map.put("appId", appId);
map.put("signType", "1");
App app = appService.selectApp(Integer.valueOf(appId));
Assert.assertNotNull("app should not null", app);
secureManager.doAppSign(map, Sets.newHashSet("sign"), app.getSignKey());
Assert.assertNotNull("sign should not null", map.get("sign"));
MvcResult result = this.mockMvc.perform(
post("/api/pay/cashierOrder")
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.content(new ObjectMapper().writeValueAsString(map))
)
.andExpect(status().isOk())
.andReturn();
System.out.println(result.getResponse().getContentAsString());
}
private String sign(Map<String, String> map) throws Exception {
UriComponentsBuilder builder = UriComponentsBuilder
.fromPath("/api/pay/sign");
for (Map.Entry<String, String> entry : map.entrySet()) {
builder.queryParam(entry.getKey(), entry.getValue());
}
System.out.println(builder.toUriString());
MvcResult result = this.mockMvc.perform(get(builder.toUriString())
.accept(MediaType.parseMediaType(MediaType.APPLICATION_JSON_UTF8_VALUE)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.sign").exists())
.andReturn();
String sign = JsonPath.read(
result.getResponse().getContentAsString(),
"$.data.sign"
);
System.out.println("----- sign done -----");
System.out.println();
System.out.println("----- sign done -----");
return sign;
}
}
以上是关于java [CashierControllerTest] Spring Controller单元测试类#java #spring的主要内容,如果未能解决你的问题,请参考以下文章