您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > junit
Junit單元測(cè)試簡(jiǎn)單小例子
作者:帥姐 發(fā)布時(shí)間:[ 2016/8/15 14:09:32 ] 推薦標(biāo)簽:單元測(cè)試 Junit

  一、編寫一個(gè)web程序
  本文根據(jù)工作實(shí)際項(xiàng)目,編寫一個(gè)web程序:實(shí)現(xiàn)防偽碼查詢頁(yè)面,輸入防偽碼,點(diǎn)擊查詢,查詢數(shù)據(jù)庫(kù)并作出判斷,將結(jié)果顯示在查詢頁(yè)面。

  1、需要添加的buildpath:ojdbc14.jarclasses12.jar

  2、編寫searchresult.jsp文件:(剛?cè)腴T格式不規(guī)范)
1 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
2 <!--%
3     String path = request.getContextPath();
4     String basePath = request.getScheme() + "://"
5             + request.getServerName() + ":" + request.getServerPort()
6             + path + "/";
7 %>-->
8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
9 <%@ page import="java.sql.*"%>
10 <%@ page import="testsample001.check"%>
11 <html>
12 <head>
13 <!--base href="<!--%=basePath%>">-->
14 <title>查詢結(jié)果</title>
15 <meta http-equiv="pragma" content="no-cache">
16 <meta http-equiv="cache-control" content="no-cache">
17 <meta http-equiv="expires" content="0">
18 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
19 <meta http-equiv="description" content="This is my page">
20 <!--<link rel="stylesheet" type="text/css" href="styles.css"> -->
21 </head>
22 <body>
23     <br>
24     <br>
25     <br>
26     <table>
27         <tr>
28             <td>
29                 <form action="/testsample001/testservlet" method="post">
30                     防偽碼
31                     <!--input name="fangweima" id="t1" type="text">-->
32                     <!--input name="searchbtn" type="button" value='查詢' onclick="check1()">-->
33                     <!--input name="result" id="t2" type="text"-->
34                     <input type="text" id="t1" name="t1" value="<%=session.getAttribute("t1")%>" />
35                     <!--input type="text" id="t2" name="t2" value="<%=session.getAttribute("t2")%>"/>-->
36                     <input type="submit" value="查詢"/>
37                     <br>
38                     查詢結(jié)果:<%=session.getAttribute("t2")%>;
39                 </form>
40             </td>
41         </tr>
42     </table>
43 </body>
44 </html>
  3、編寫check.java文件
1 package testsample001;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.sql.Statement;
8
9 public class check {
10     private Connection con;
11     private String user = "deejuser";
12     // private String user = "sys as sysdba";
13     private String password = "deejuser";
14     private String className = "oracle.jdbc.driver.OracleDriver";
15     // private String url="jdbc:oracle:oci@localhost:1158:orcl";這個(gè)url可能無效
16     private String url = "jdbc:oracle:thin:@zs-PC:1521:ytdf";
17
18     public String ConnectOracle(String productno) {
19         try {
20             Class.forName(className);
21             // System.out.println("加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)成功!");
22             // System.out.println(productno);
23             String resultInfo = getCon(productno);
24             closed();
25             return resultInfo;
26         } catch (ClassNotFoundException e) {
27             // System.out.println("加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)失!");
28             e.printStackTrace();
29             return "加載數(shù)據(jù)庫(kù)驅(qū)動(dòng)失!";
30         }
31     }
32
33     /** 創(chuàng)建數(shù)據(jù)庫(kù)連接 */
34     // public Connection getCon(String productno) {
35     public String getCon(String productno) {
36         try {
37             con = DriverManager.getConnection(url, user, password);
38             // System.out.println("創(chuàng)建數(shù)據(jù)庫(kù)連接成功!");
39             Statement stmt = con
40                     .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
41                             ResultSet.CONCUR_UPDATABLE);
42             String sql = "select * from product t where t.packagsn="
43                     + productno;
44             // System.out.print(sql);
45             ResultSet rs = stmt.executeQuery(sql);
46             if (rs.next())
47                 return "防偽碼存在!";
48             else
49                 return "防偽碼不存在!";
50         } catch (SQLException e) {
51             // System.out.print(con);
52             // System.out.println("創(chuàng)建數(shù)據(jù)庫(kù)連接失敗!");
53             con = null;
54             e.printStackTrace();
55             return "數(shù)據(jù)庫(kù)連接失!";
56         }
57         // return con;
58     }
59
60     public void closed() {
61         try {
62             if (con != null) {
63                 con.close();
64             }
65         } catch (SQLException e) {
66             System.out.println("關(guān)閉con對(duì)象失。");
67             e.printStackTrace();
68         }
69     }
70 }
  4、編寫testservlet.java文件
1 package testsample002;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.annotation.WebServlet;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import testsample001.check;
12
13 /**
14  * Servlet implementation class testservlet
15  */
16 @WebServlet("/testservlet")
17 public class testservlet extends HttpServlet {
18     private static final long serialVersionUID = 1L;
19
20     /**
21      * @see HttpServlet#HttpServlet()
22      */
23     public testservlet() {
24         super();
25         // TODO Auto-generated constructor stub
26     }
27
28     /**
29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
30      *      response)
31      */
32     protected void doGet(HttpServletRequest request,
33             HttpServletResponse response) throws ServletException, IOException {
34         // TODO Auto-generated method stub
35     }
36
37     /**
38      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
39      *      response)
40      */
41     protected void doPost(HttpServletRequest request,
42             HttpServletResponse response) throws ServletException, IOException {
43         // TODO Auto-generated method stub
44         String value = request.getParameter("t1");// 獲取前臺(tái)頁(yè)面?zhèn)鬟f過來的數(shù)據(jù)
45         // request.getSession(false).setAttribute("t1", value);// 將數(shù)據(jù)放到session中
46         check connor = new check();
47         String resultInfo = connor.ConnectOracle(value);
48         request.getSession(false).setAttribute("t2", resultInfo);// 將數(shù)據(jù)放到session中
49         request.getSession(false).setAttribute("t1", value);
50         response.sendRedirect("/testsample001/searchresult.jsp");// 跳轉(zhuǎn)回提交的頁(yè)面
51
52     }
53
54 }
  5、設(shè)置項(xiàng)目的起始頁(yè)面(tomcat服務(wù)配置-web.xml)

  1     <welcome-file-list>
  2         <welcome-file>index.html</welcome-file>
  3         <welcome-file>index.htm</welcome-file>
  4         <welcome-file>searchresult.jsp</welcome-file>
  5     </welcome-file-list>
  6、實(shí)現(xiàn)結(jié)果:

  7、導(dǎo)出jar包(在要生成jar的項(xiàng)目上右擊,選擇菜單上的Export(導(dǎo)出)),導(dǎo)出oracleconnect.jar

上一頁(yè)12下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd