您的位置:首页 > 其它

接口测试中 对xml下发内容的解析断言

2012-08-07 22:24 393 查看
import com.app.util.EncryptUtil;

import com.juyun.test.util.Java2Xml;

import com.juyun.test.util.XmlUtil;

public class TestgetKeyword {

****

BufferedReader reader=null;

@Before

public void setUpBeforeEveryTime() throws Exception{

// flushCache.FlushAll(host, port);

System.out.println("+++setUpBefore");

System.out.println("flush cache...");

System.out.println("===========================");

}

@Test

public void testGetVersion() throws Exception {

httpClient = new HttpClient();

EntityEnclosingMethod postMethod = new PostMethod();

// String string = xUtil.xmltoString(file);

String string = getDocument();

byte[] request = string.getBytes("UTF-8");

byte[] requestcode =EncryptUtil.getInstance().encode(request);

RequestEntity requestEntity = new ByteArrayRequestEntity(requestcode);

postMethod.setRequestEntity(requestEntity);

postMethod.setPath(url);

postMethod.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30*1000);

httpClient.executeMethod(postMethod);

byte[] responsecode =postMethod.getResponseBody();

byte[] response =EncryptUtil.getInstance().decode(responsecode);

ByteArrayEntity byteArrayEntity = new ByteArrayEntity(response) ;

reader = new BufferedReader(new InputStreamReader(byteArrayEntity.getContent()));

String xml = reader.readLine();

Document document =xUtil.StringtoXml(xml);

// System.out.println(document.asXML());

assertEquals("<result_code>1</result_code>", document.selectSingleNode("//result_code").asXML());

// assertEquals("<appid>894</appid>", document.selectSingleNode("//entry_total/appid").asXML());

}

@AfterClass //执行一次

public static void tearDownAfterClassOnce() throws Exception {

System.out.println("===========================");

System.out.println("tearDownAfterClass");

}

}

直接对document 进行内容选取,再对相关节点断言判断,以此来测试接口下发正确性
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: