TestRestTemplate对REST接口进行单元测试示例

1,依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

2,测试抽象类

import org.junit.Assert;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;

import com.hsjry.base.common.model.constant.RestConstant;
import com.hsjry.base.common.model.dto.base.CommonResponse;
import com.hsjry.base.common.model.enums.common.EnumResponseType;

/**
 * @author Csdn6153
 * @since 2.1.0 2019-03-26 16:07
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class AbstractBaseTest {

    @LocalServerPort
    protected int port;

    @Autowired
    protected TestRestTemplate restTemplate;

    private final String serialNo = "Junit_" + System.currentTimeMillis();

    @Before
    public void setUp() {
        //   初始化自定义请求头
    }

    /**
     * 获取默认的请求头
     *
     * @return 请求头
     */
    public HttpHeaders getDefaultHttpHeader() {
        HttpHeaders headers = new HttpHeaders();
        // 默认json
        headers.add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_UTF8_VALUE);
        // 自动以请求头
        // headers.add("", "");

        return headers;
    }

    /**
     * @param url 请求路径
     * @param entity 请求参数
     */
    public void testAssertApi(String url, HttpEntity<?> entity) {
        ResponseEntity<CommonResponse> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity,
            CommonResponse.class);

        Assert.assertNotNull(responseEntity.getBody());
        Assert.assertNull(responseEntity.getBody()
            .getErrorCode());
        Assert.assertEquals(EnumResponseType.SUCCESS, responseEntity.getBody()
            .getResponseType());
    }

    /**
     * 获取访问url
     *
     * @param path
     * @return
     */
    public String getUrl(String path) {
        String controllerPath = RestConstant.MODULE_OP_CHANNEL_PREFIX;
        return String.format("http://localhost:%d" + controllerPath + path, port);
    }

}

3,controller

/**
 * @author Csdn6153
 * @since 2.1.0 2019-03-26 16:30
 */
@Slf4j
@RestController
@RequestMapping(RestConstant.MODULE_OP_CHANNEL_PREFIX + "/v1")
public class DemoController {

    @Autowired
    private DemoBiz biz;

    /**
     * 初始化数据 导入还款
     *
     * @param file 文件
     * @return 出参
     */
    @PostMapping("/imports")
    public CommonResponse imports(MultipartFile file) throws IOException {
        biz.imports(file.getOriginalFilename(), file.getBytes());
        return CommonResponse.builder()
            .build();
    }

}

4,测试demo类

/**
 * @author Csdn6153
 * @since 2.1.0 2019-03-26 17:07
 */
@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoControllerTest extends AbstractBaseTest {

    /**
     * @see DemoController
     */
@Test
public void testDemo() throws Exception {
    String path = "";
    MultiValueMap<String, Object> multipartMap = new LinkedMultiValueMap<>();
    multipartMap.add("file", new FileSystemResource(path));
    HttpEntity<Object> entity = new HttpEntity<>(multipartMap, getDefaultHttpHeader());
    String url = getUrl("/v1/imports");
    testAssertApi(url, entity);
    System.out.println(11);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值