1、安裝插件JUnitGenertor V2.0java
File->Setting->Plugins->在搜索框裏輸入JUintGenerator V2.0app
2、導入JUnit相關jar包單元測試
1. junit-4.12.jar測試
2. hamcrest-core-1.3.jarthis
3、待測試類添加JUnit測試類編碼
打開待測試類,按快捷鍵 ALT+INSERT,選擇JUnit 4url
4、編寫測試代碼spa
demo展現:.net
下面是一個單元測試驅動器,根據參數狀況分別調用Servlet(待測試類)的方法來進行斷言驗證。插件
import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Map; import static org.junit.Assert.assertEquals; /** * Servlet Tester. * * @author <Authors name> * @since <pre>四月 3, 2019</pre> * @version 1.0 */ @RunWith(Parameterized.class) public class ServletTest { private Map<String, String> parameters; // 定義參數映射集合 private String result; // 預期結果
@Parameterized.Parameters public static Collection data() {
// 實例化參數映射集合,參數就是測試用例中的參數 Map<String , String> parameters1 = new HashMap<String , String>(){{ // 查詢全部書籍模塊參數 put("name", "虛擬"); put("msg", "0"); }}; Map<String , String> parameters2 = new HashMap<String , String>(){{ // 查詢某個書籍詳情模塊參數 put("id", "1"); put("msg", "1"); }}; Map<String , String> parameters3 = new HashMap<String , String>(){{ // 登錄模塊參數 put("name", "hey"); put("password", "hey123"); put("msg", "2"); }}; Map<String , String> parameters4 = new HashMap<String , String>(){{ // 借書參數 put("id", "1"); put("msg", "3"); }}; return Arrays.asList(new Object[][]{ {"{\"address\":\"\",\"author\":\"周志明\",\"detail\":\"\",\"id\":1,\"name\":\"沒法理解虛擬機\",\"press\":\"北京大出版社\",\"pressdate\":\"2019-03-06\",\"status\":\"\"}",parameters1}, {"{\"address\":\"P0A1\",\"author\":\"\",\"detail\":\"很好\",\"id\":1,\"name\":\"沒法理解虛擬機\",\"press\":\"\",\"pressdate\":\"\",\"status\":\"已出館\"}",parameters2}, {"1",parameters3}, {"1",parameters4}, }); } public ServletTest(String result,Map<String, String> parameters) { this.parameters = parameters;
this.result = result;
} @Before public void before() throws Exception { System.out.println("開始測試...."); } @After public void after() throws Exception { } /** * * Method: doPost(HttpServletRequest request, HttpServletResponse response) * */ @Test public void testDoPost() throws Exception { //TODO: Test goes here... } /** * * Method: doGet(HttpServletRequest request, HttpServletResponse response) * */ @Test public void testDoGet() throws Exception { assertEquals(result,sendPost(parameters)); } public static String sendPost(Map<String, String> parameters) { String result = "";// 返回的結果 BufferedReader in = null;// 讀取響應輸入流 PrintWriter out = null; StringBuffer sb = new StringBuffer();// 處理請求參數 String params = "";// 編碼以後的參數 try { // 編碼請求參數 if (parameters.size() == 1) { for (String name : parameters.keySet()) { sb.append(name).append("=").append( java.net.URLEncoder.encode(parameters.get(name), "UTF-8")); } params = sb.toString(); } else { for (String name : parameters.keySet()) { sb.append(name).append("=").append( java.net.URLEncoder.encode(parameters.get(name), "UTF-8")).append("&"); } String temp_params = sb.toString(); params = temp_params.substring(0, temp_params.length() - 1); } String url = "http://localhost:8080/Servlet"; // 建立URL對象 java.net.URL connURL = new java.net.URL(url); // 打開URL鏈接 java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL .openConnection(); // 設置通用屬性 httpConn.setRequestProperty("Accept", "*/*"); httpConn.setRequestProperty("Connection", "Keep-Alive"); httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)"); // 設置POST方式 httpConn.setDoInput(true); httpConn.setDoOutput(true); // 獲取HttpURLConnection對象對應的輸出流 out = new PrintWriter(httpConn.getOutputStream()); // 發送請求參數 out.write(params); // flush輸出流的緩衝 out.flush(); // 定義BufferedReader輸入流來讀取URL的響應,設置編碼方式 in = new BufferedReader(new InputStreamReader(httpConn .getInputStream(), "UTF-8")); String line; // 讀取返回的內容 while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } }