junit测试--访问其他主机的IP的测试代码

本文介绍了一种使用Java进行邮件发送API测试的方法。通过构造EmailSendApiInput对象并将其序列化为JSON,然后通过HttpPost方式调用指定API完成邮件发送。测试中详细设置了邮件的各种属性,如发件人、收件人、抄送人等。
摘要由CSDN通过智能技术生成
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);
                  }





}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值