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); }
}