spring boot using TestRestTemplate integration testing RESTful interface

Simply record how access api security protected with TestRestTemplate, for later reference.

 

  1.  
    @ Slf4j
  2.  
    @RunWith(SpringRunner.class)
  3.  
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  4.  
    public class AccountControllerTests {
  5.  
    @Autowired
  6.  
    Private Test Rest Template rest template;
  7.  
    private HttpEntity httpEntity;
  8.  
     
  9.  
    /**
  10.  
    * log in
  11.  
    * @throws Exception
  12.  
    */
  13.  
    private void login() throws Exception {
  14.  
    String expectStr = "{\"code\":0,\"msg\":\"success\"}";
  15.  
    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
  16.  
    map.add( "username", "183xxxxxxxx");
  17.  
    map.add( "password", "123456");
  18.  
    ResponseEntity responseEntity = restTemplate.postForEntity( "/api/account/sign_in", map, String.class);
  19.  
    // Add the cookie to maintain state
  20.  
    HttpHeaders headers = new HttpHeaders();
  21.  
    String headerValue = responseEntity.getHeaders().get( "Set-Cookie").toString().replace("[", "");
  22.  
    headerValue = headerValue.replace( "]", "");
  23.  
    headers.set( "Cookie", headerValue);
  24.  
    httpEntity = new HttpEntity(headers);
  25.  
    assertThat(responseEntity.getBody()).isEqualTo(expectStr);
  26.  
    }
  27.  
     
  28.  
    /**
  29.  
    * Sign out
  30.  
    * @throws Exception
  31.  
    */
  32.  
    private void logout() throws Exception {
  33.  
    String expectStr = "{\"code\":0,\"msg\":\"success\"}";
  34.  
    String result = restTemplate.postForObject( "/api/account/sign_out", null, String.class, httpEntity);
  35.  
    httpEntity = null;
  36.  
    assertThat(result).isEqualTo(expectStr);
  37.  
    }
  38.  
     
  39.  
    /**
  40.  
    * getting information
  41.  
    * @throws Exception
  42.  
    */
  43.  
    private void getUserInfo() throws Exception {
  44.  
    Detail detail = new Detail();
  45.  
    detail.setNickname ( "Crazy Mickey Mouse");
  46.  
    detail.setNicknamePinyin( "fengkuangdemilaoshu");
  47.  
    detail.setSex( 1);
  48.  
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  49.  
    detail.setCreatedAt(sdf.parse( "2017-11-03 16:43:27"));
  50.  
    detail.setUpdatedAt(sdf.parse( "2017-11-03 16:43:27"));
  51.  
    Role role = new Role();
  52.  
    role.setName( "ROLE_USER_NORMAL");
  53.  
    Set<Role> roles = new HashSet<>();
  54.  
    roles.add(role);
  55.  
    User user = new User();
  56.  
    user.setId( 1L);
  57.  
    user.setPhone( "183xxxxxxxx");
  58.  
    user.setEmail( "[email protected]");
  59.  
    user.setDetail(detail);
  60.  
    user.setRoles(roles);
  61.  
    ResultBean<User> resultBean = new ResultBean<>();
  62.  
    resultBean.setData(user);
  63.  
    ObjectMapper om = new ObjectMapper();
  64.  
    String expectStr = om.writeValueAsString(resultBean);
  65.  
    ResponseEntity<String> responseEntity = restTemplate.exchange( "/api/user/get_user_info", HttpMethod.GET, httpEntity, String.class);
  66.  
    assertThat(responseEntity.getBody()).isEqualTo(expectStr);
  67.  
    }
  68.  
     
  69.  
    @Test
  70.  
    public void testAccount() throws Exception {
  71.  
    login();
  72.  
    getUserInfo();
  73.  
    logout();
  74.  
    }

Guess you like

Origin www.cnblogs.com/zgq123456/p/12614545.html
Recommended