重新配置集成测试 #9
@@ -2,12 +2,15 @@ package cn.celess.blog;
|
|||||||
|
|
||||||
|
|
||||||
import cn.celess.blog.entity.Response;
|
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.model.UserModel;
|
||||||
import cn.celess.blog.entity.request.LoginReq;
|
import cn.celess.blog.entity.request.LoginReq;
|
||||||
import cn.celess.blog.service.MailService;
|
import cn.celess.blog.service.MailService;
|
||||||
|
import cn.celess.blog.service.QiniuService;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.qiniu.storage.model.FileInfo;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -32,6 +35,7 @@ import org.springframework.web.context.WebApplicationContext;
|
|||||||
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -273,12 +277,12 @@ public class BaseTest {
|
|||||||
* @param service service 类
|
* @param service service 类
|
||||||
* @param mailFiledName service 中自动注入的mailService字段名
|
* @param mailFiledName service 中自动注入的mailService字段名
|
||||||
*/
|
*/
|
||||||
protected void mockEmailServiceInstance(Object service, String mailFiledName) {
|
protected void mockInjectInstance(Object service, String mailFiledName, Object impl) {
|
||||||
Field mailServiceField;
|
Field field;
|
||||||
try {
|
try {
|
||||||
mailServiceField = service.getClass().getDeclaredField(mailFiledName);
|
field = service.getClass().getDeclaredField(mailFiledName);
|
||||||
mailServiceField.setAccessible(true);
|
field.setAccessible(true);
|
||||||
mailServiceField.set(service, new TestMailServiceImpl());
|
field.set(service, impl);
|
||||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -286,7 +290,7 @@ public class BaseTest {
|
|||||||
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
protected static class TestMailServiceImpl implements MailService {
|
public static class TestMailServiceImpl implements MailService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Boolean AsyncSend(SimpleMailMessage message) {
|
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
|
@Test
|
||||||
public void apply() {
|
public void apply() {
|
||||||
// 做service 层的测试
|
// 做service 层的测试
|
||||||
mockEmailServiceInstance(partnerSiteService, "mailService");
|
// mockEmailServiceInstance(partnerSiteService, "mailService");
|
||||||
|
mockInjectInstance(partnerSiteService, "mailService", new TestMailServiceImpl());
|
||||||
LinkApplyReq req = new LinkApplyReq();
|
LinkApplyReq req = new LinkApplyReq();
|
||||||
req.setName(randomStr(4));
|
req.setName(randomStr(4));
|
||||||
req.setUrl("https://" + randomStr(4) + ".celess.cn");
|
req.setUrl("https://" + randomStr(4) + ".celess.cn");
|
||||||
@@ -224,7 +225,7 @@ public class LinksControllerTest extends BaseTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void reapply() {
|
public void reapply() {
|
||||||
mockEmailServiceInstance(partnerSiteService, "mailService");
|
//mockEmailServiceInstance(partnerSiteService, "mailService");
|
||||||
try {
|
try {
|
||||||
partnerSiteService.reapply(randomStr());
|
partnerSiteService.reapply(randomStr());
|
||||||
throw new AssertionError();
|
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.LoginReq;
|
||||||
import cn.celess.blog.entity.request.UserReq;
|
import cn.celess.blog.entity.request.UserReq;
|
||||||
import cn.celess.blog.mapper.UserMapper;
|
import cn.celess.blog.mapper.UserMapper;
|
||||||
|
import cn.celess.blog.service.UserService;
|
||||||
import cn.celess.blog.util.MD5Util;
|
import cn.celess.blog.util.MD5Util;
|
||||||
import net.sf.json.JSONArray;
|
import net.sf.json.JSONArray;
|
||||||
import net.sf.json.JSONObject;
|
import net.sf.json.JSONObject;
|
||||||
@@ -24,13 +25,14 @@ import java.util.*;
|
|||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
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.*;
|
import static cn.celess.blog.enmu.ResponseEnum.*;
|
||||||
|
|
||||||
public class UserControllerTest extends BaseTest {
|
public class UserControllerTest extends BaseTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
UserMapper userMapper;
|
UserMapper userMapper;
|
||||||
|
@Autowired
|
||||||
|
UserService userService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void login() throws Exception {
|
public void login() throws Exception {
|
||||||
@@ -99,6 +101,10 @@ public class UserControllerTest extends BaseTest {
|
|||||||
connection.setRequestMethod("GET");
|
connection.setRequestMethod("GET");
|
||||||
InputStream inputStream = connection.getInputStream();
|
InputStream inputStream = connection.getInputStream();
|
||||||
assertNotNull(inputStream);
|
assertNotNull(inputStream);
|
||||||
|
|
||||||
|
// mock 实现类
|
||||||
|
mockInjectInstance(userService, "qiniuService", new TestQiNiuServiceImpl());
|
||||||
|
|
||||||
MockMultipartFile file = new MockMultipartFile("file", "logo.png", MediaType.IMAGE_PNG_VALUE, 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 -> {
|
||||||
|
|||||||
Reference in New Issue
Block a user