Java测试-RestTemplate-@requestBody

在java的test代码中,如果遇到了要传入@requestBody声明的参数时,可以使用以下代码

String infra = "{\"upperId\":110000,\"areaName\":\"测试BaseArea\",\"remark\":\"备注\"}";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.valueOf("application/json;UTF-8"));
HttpEntity<String> strEntity = new HttpEntity<String>(infra,headers);

RestTemplate restTemplate = new RestTemplate();
JSONObject jo2 = restTemplate.postForObject(url,strEntity,JSONObject.class);
System.out.println(jo2);

infra为json字符串

strEntity为传递用的参数


提取自:http://blog.csdn.net/qwe6112071/article/details/51042634



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
测试单点登录(SSO)的实现,你可以使用Java编写单元测试来验证登录功能是否正常工作。以下是一个简单的示例: 1. 首先,确保你的应用程序已经实现了单点登录功能,并且你已经有一个可以进行登录的页面。 2. 创建一个测试类,例如`SSOTest`,并在该类上添加`@RunWith(SpringRunner.class)`和`@SpringBootTest`注解,以便使用Spring Boot进行集成测试。 ```java import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.client.RestTemplate; @RunWith(SpringRunner.class) @SpringBootTest public class SSOTest { @Autowired private RestTemplate restTemplate; @Test public void testLogin() { String username = "your_username"; String password = "your_password"; // 构建登录请求 String loginUrl = "http://localhost:8080/login"; // 替换为你的登录页面URL String requestBody = "username=" + username + "&password=" + password; ResponseEntity<String> response = restTemplate.postForEntity(loginUrl, requestBody, String.class); // 验证登录结果 Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); // 如果登录成功后,可以获取响应中的Cookie或其他标识来验证登录状态 // ... } } ``` 3. 在测试方法中,你可以使用`RestTemplate`执行登录请求,并验证返回的响应状态码和登录结果。 4. 运行测试类,你应该能够看到测试结果。如果登录成功,响应状态码应该是200 OK,并且可以根据实际情况验证登录状态。 请注意,这只是一个简单的示例,实际的测试可能会更加复杂,具体取决于你的应用程序的实现方式和需求。你可以根据自己的情况进行适当调整和扩展。另外,还可以使用其他测试工具和框架(如JUnit、MockMvc等)来进行更全面的测试

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值