修改获取测试资源的方式

This commit is contained in:
小海
2019-11-29 23:58:55 +08:00
parent 55ccff3451
commit 1f736dc9c9

View File

@@ -14,10 +14,9 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile; import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import java.io.InputStream;
import java.net.HttpURLConnection;
import java.io.File; import java.net.URL;
import java.io.FileInputStream;
import java.util.*; import java.util.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@@ -93,8 +92,12 @@ public class UserControllerTest extends BaseTest {
@Test @Test
public void upload() throws Exception { public void upload() throws Exception {
File logoFile = new File("C:\\Users\\zh564\\Pictures\\logo.png"); URL url = new URL("https://56462271.oss-cn-beijing.aliyuncs.com/web/logo.png");
MockMultipartFile file = new MockMultipartFile("file", "logo.png", MediaType.IMAGE_PNG_VALUE, new FileInputStream(logoFile)); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
assertNotNull(inputStream);
MockMultipartFile file = new MockMultipartFile("file", "logo.png", MediaType.IMAGE_PNG_VALUE, inputStream);
mockMvc.perform(multipart("/user/imgUpload").file(file)).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code))); mockMvc.perform(multipart("/user/imgUpload").file(file)).andDo(result -> assertEquals(HAVE_NOT_LOG_IN.getCode(), JSONObject.fromObject(result.getResponse().getContentAsString()).getInt(Code)));
mockMvc.perform(multipart("/user/imgUpload").file(file).header("Authorization", userLogin())).andDo(result -> { mockMvc.perform(multipart("/user/imgUpload").file(file).header("Authorization", userLogin())).andDo(result -> {
JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString()); JSONObject object = JSONObject.fromObject(result.getResponse().getContentAsString());