refactor(Test): 修改上传文件到图床的实现方式
This commit is contained in:
@@ -2,12 +2,15 @@ package cn.celess.blog;
|
||||
|
||||
|
||||
import cn.celess.blog.entity.Response;
|
||||
import cn.celess.blog.entity.model.QiniuResponse;
|
||||
import cn.celess.blog.entity.model.UserModel;
|
||||
import cn.celess.blog.entity.request.LoginReq;
|
||||
import cn.celess.blog.service.MailService;
|
||||
import cn.celess.blog.service.QiniuService;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.qiniu.storage.model.FileInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -32,6 +35,7 @@ import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Map;
|
||||
@@ -273,12 +277,12 @@ public class BaseTest {
|
||||
* @param service service 类
|
||||
* @param mailFiledName service 中自动注入的mailService字段名
|
||||
*/
|
||||
protected void mockEmailServiceInstance(Object service, String mailFiledName) {
|
||||
Field mailServiceField;
|
||||
protected void mockInjectInstance(Object service, String mailFiledName, Object impl) {
|
||||
Field field;
|
||||
try {
|
||||
mailServiceField = service.getClass().getDeclaredField(mailFiledName);
|
||||
mailServiceField.setAccessible(true);
|
||||
mailServiceField.set(service, new TestMailServiceImpl());
|
||||
field = service.getClass().getDeclaredField(mailFiledName);
|
||||
field.setAccessible(true);
|
||||
field.set(service, impl);
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -286,7 +290,7 @@ public class BaseTest {
|
||||
|
||||
|
||||
@Slf4j
|
||||
protected static class TestMailServiceImpl implements MailService {
|
||||
public static class TestMailServiceImpl implements MailService {
|
||||
|
||||
@Override
|
||||
public Boolean AsyncSend(SimpleMailMessage message) {
|
||||
@@ -316,4 +320,25 @@ public class BaseTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Slf4j
|
||||
public static class TestQiNiuServiceImpl implements QiniuService {
|
||||
@Override
|
||||
public QiniuResponse uploadFile(InputStream is, String fileName) {
|
||||
QiniuResponse response = new QiniuResponse();
|
||||
log.debug("上传文件请求,[fileName:{}]", fileName);
|
||||
|
||||
response.key = "key";
|
||||
response.bucket = "bucket";
|
||||
response.hash = "hash";
|
||||
response.fsize = 1;
|
||||
return response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo[] getFileList() {
|
||||
log.debug("获取文件列表请求");
|
||||
return new FileInfo[0];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -182,7 +182,8 @@ public class LinksControllerTest extends BaseTest {
|
||||
@Test
|
||||
public void apply() {
|
||||
// 做service 层的测试
|
||||
mockEmailServiceInstance(partnerSiteService, "mailService");
|
||||
// mockEmailServiceInstance(partnerSiteService, "mailService");
|
||||
mockInjectInstance(partnerSiteService, "mailService", new TestMailServiceImpl());
|
||||
LinkApplyReq req = new LinkApplyReq();
|
||||
req.setName(randomStr(4));
|
||||
req.setUrl("https://" + randomStr(4) + ".celess.cn");
|
||||
@@ -224,7 +225,7 @@ public class LinksControllerTest extends BaseTest {
|
||||
|
||||
@Test
|
||||
public void reapply() {
|
||||
mockEmailServiceInstance(partnerSiteService, "mailService");
|
||||
//mockEmailServiceInstance(partnerSiteService, "mailService");
|
||||
try {
|
||||
partnerSiteService.reapply(randomStr());
|
||||
throw new AssertionError();
|
||||
|
||||
@@ -7,6 +7,7 @@ import cn.celess.blog.entity.model.UserModel;
|
||||
import cn.celess.blog.entity.request.LoginReq;
|
||||
import cn.celess.blog.entity.request.UserReq;
|
||||
import cn.celess.blog.mapper.UserMapper;
|
||||
import cn.celess.blog.service.UserService;
|
||||
import cn.celess.blog.util.MD5Util;
|
||||
import net.sf.json.JSONArray;
|
||||
import net.sf.json.JSONObject;
|
||||
@@ -24,13 +25,14 @@ import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||
|
||||
public class UserControllerTest extends BaseTest {
|
||||
|
||||
@Autowired
|
||||
UserMapper userMapper;
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
@Test
|
||||
public void login() throws Exception {
|
||||
@@ -99,6 +101,10 @@ public class UserControllerTest extends BaseTest {
|
||||
connection.setRequestMethod("GET");
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
assertNotNull(inputStream);
|
||||
|
||||
// mock 实现类
|
||||
mockInjectInstance(userService, "qiniuService", new TestQiNiuServiceImpl());
|
||||
|
||||
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).header("Authorization", userLogin())).andDo(result -> {
|
||||
|
||||
Reference in New Issue
Block a user