1、功能测试类
package com.imooc.controller;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.Assert;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import com.imooc.entity.Product;
import com.imooc.entity.enums.ProductStatus;
import com.imooc.util.RestUtil;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
@FixMethodOrder(MethodSorters.NAME_ASCENDING) // case执行顺序
public class ProductControllerTest {
// @Autowired
// private TestRestTemplate rest;
private static RestTemplate rest = new RestTemplate();
@Value("http://localhost:${local.server.port}/

本文主要介绍如何运用JUnit进行功能测试,重点在于SpringBoot应用中使用RestTemplate测试Controller。首先,我们创建功能测试类,接着利用RestTemplate进行HTTP请求模拟,对Controller的各个接口进行验证和测试。
最低0.47元/天 解锁文章
3380

被折叠的 条评论
为什么被折叠?



