获取网页内容的方法:
public class NetWorkUtil { public static String getHTMLContent(String site) { URL url = null; try { url = new URL(site); } catch (MalformedURLException e) { e.printStackTrace(); } StringBuilder content = null; try { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF8")); content = new StringBuilder(); String temp = null; while((temp = br.readLine()) != null) { content.append(temp); } } catch (IOException e) { e.printStackTrace(); } return content.toString(); } }
测试类:
public class TestRunner { private String site; @Before public void setUp() throws Exception { site = "http://www.soft.zjut.edu.cn:8080"; } @After public void tearDown() throws Exception { site = null; } @Test public void test() { System.out.println(NetWorkUtil.getHTMLContent(site)); } }
执行:
查看执行时间:
(21.227秒)
如果规定测试方法的最多执行时间:
@Test(timeout=1000) (单位:毫秒)
public class TestRunner { private String site; @Before public void setUp() throws Exception { site = "http://www.soft.zjut.edu.cn:8080"; } @After public void tearDown() throws Exception { site = null; } @Test(timeout = 3000) public void test() { System.out.println(NetWorkUtil.getHTMLContent(site)); } }
(test timed out after 3000 milliseconds)