package com.juneyaoair.platform.controller.api;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
import java.util.Date;
import javax.annotation.Resource;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;
import com.juneyaoair.platform.model.api.EmailSendApiInput;
import com.mars.fw.util.json.JsonUtil;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:config/spring-mvc.xml",
"classpath:config/spring-resources.xml",
"classpath:config/spring-application.xml",
"classpath:config/spring-security.xml",
"classpath:config/spring-redis.xml" })
public class EmailSendApiControllerTest {
@Resource
WebApplicationContext wac;
MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.wac).build();
}
@Test
public void emailSendTest3() throws Exception {
EmailSendApiInput input = new EmailSendApiInput();
input.setUserId("wcl");
input.setPassword("123456");
input.setSystemId("E0001");
input.setEmailType("T01");
input.setRecipient("11111@qq.com");
input.setCc("111111111@qq.com");
input.setBcc("");
input.setSubject("我是测试主题");
input.setBodyContent("我是测试内容");
input.setIsBodyHtml("1");
input.setPriority(2);
input.setCharset("UTF-8");
input.setExpiredTime("2017-02-19 10:10:10");
input.setScheduleTime("");
input.setAttId("A2017030600000019");
// 转为json
String json = JsonUtil.toJson(input);
// 需要访问的API地址
String url = "http://192.168.0.111:8080/platform/email/send";
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8");
StringEntity se = new StringEntity(json, "UTF-8");
se.setContentType("text/json");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf-8"));
httpPost.setEntity(se);
HttpResponse httpResponse = httpClient.execute(httpPost);
// 获取返回参数
String resp = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
System.out.println(resp);
}
}
junit测试--访问其他主机的IP的测试代码
最新推荐文章于 2023-05-25 00:46:22 发布
本文介绍了一种使用Java进行邮件发送API测试的方法。通过构造EmailSendApiInput对象并将其序列化为JSON,然后通过HttpPost方式调用指定API完成邮件发送。测试中详细设置了邮件的各种属性,如发件人、收件人、抄送人等。
摘要由CSDN通过智能技术生成