BaseControllerTest.java 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.zd.system.base;
  2. import com.alibaba.fastjson.JSON;
  3. import org.junit.jupiter.api.TestInstance;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.http.HttpHeaders;
  10. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
  11. import org.springframework.test.web.servlet.MockMvc;
  12. import org.springframework.util.Assert;
  13. import org.springframework.web.client.RestTemplate;
  14. import java.util.Collections;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. /**
  18. * 测试controller 基类
  19. *
  20. * @Author: zhoupan
  21. * @Date: 2021/09/08/8:39
  22. * @Description:
  23. */
  24. //@ActiveProfiles("junittest")
  25. @TestInstance(TestInstance.Lifecycle.PER_CLASS)
  26. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  27. @AutoConfigureMockMvc
  28. public class BaseControllerTest extends AbstractTransactionalJUnit4SpringContextTests {
  29. protected final Logger log = LoggerFactory.getLogger(this.getClass());
  30. @Autowired
  31. public MockMvc mockMvc;
  32. private String bearer = "Bearer ";
  33. public static HttpHeaders httpHeaders = new HttpHeaders();
  34. /**
  35. * 登录
  36. *
  37. * @param username
  38. * @param password
  39. * @throws Exception
  40. */
  41. public void beforeAll(String username, String password, boolean f) throws Exception {
  42. //mockMve 没办法指定主机地址,所以这里改成手动请求
  43. // MvcResult mvcResult = mockMvc
  44. // .perform(MockMvcRequestBuilders.get("http://localhost:8080/code"))
  45. // .andReturn();
  46. //不再重复登录
  47. if (!f && httpHeaders.containsKey("Authorization")) {
  48. return;
  49. }
  50. RestTemplate restTemplate = new RestTemplate();
  51. //创建请求头
  52. String url = "http://localhost:8080/auth/login";
  53. Map<String, String> map = new HashMap<>(2);
  54. map.put("username", username);
  55. map.put("password", password);
  56. String body = restTemplate.postForEntity(url, map, String.class).getBody();
  57. Map<String, Object> parse = (Map<String, Object>) JSON.parse(body);
  58. Assert.isTrue(parse.get("code").toString().equals("200"), "登录失败:" + parse.get("msg"));
  59. //拿到token
  60. Map<String, String> data = (Map<String, String>) JSON.parse(parse.get("data").toString());
  61. httpHeaders.put("Authorization", Collections.singletonList(bearer + data.get("access_token")));
  62. //采用模拟方式-即不走网关
  63. httpHeaders.put("user_id", Collections.singletonList(data.get("user_id")));
  64. httpHeaders.put("username", Collections.singletonList(data.get("username")));
  65. }
  66. public void beforeAll(String username, String password) throws Exception {
  67. beforeAll(username, password, false);
  68. }
  69. }