码迷,mamicode.com
首页 > 编程语言 > 详细

Java学习十四

时间:2020-01-31 15:48:03      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:prepare   print   throw   ted   exp   成功   date   except   指定   

学习内容:

1.Junit


一、Junit实例演示步骤

1.引入jar包

技术图片

 

 

 junit包需要引入hamcrest-core包,否则会报错

2.测试如下代码

 1 package com.junit.test;
 2 
 3 public class Calculator {
 4     private static int result; //静态变量,用于存储运行结果
 5     public void add(int n){
 6         result=result+n;
 7     }
 8     public void substract(int n){
 9         result=result-1; //Bug:正确的应该是result=result-n
10     }
11     public void multiply(int n){
12         
13     }//此方法尚未写好
14     public void divide(int n)throws Exception{
15         if(n==0)
16             throw new Exception("除数不能为0");
17         result=result/n;
18     }
19     public void square(int n){
20         result=n*n;
21     }
22     public void squareRoot(int   n){
23         for(;;);         //Bug:死循环
24     }
25     public void clear(){  //将结果清零
26         result=0;
27     }
28 }

3.进行单元测试

点击测试项目,点击新建JUnit Test Case

技术图片

 

选择测试方法

 

技术图片

 演示代码

package com.junit.test;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

public class CalculatorTest {
    
    public static Calculator c = new Calculator();
    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testAdd() {
        c.add(2);
        c.add(2);
        assertEquals(4,c.getResult());
    }

    @Test
    public void testSubstract() {
        c.substract(2);
        assertEquals(2,c.getResult());
    }

    @Ignore
    public void testMultiply() {
        c.multiply(10);
    }

    @Test(expected = Exception.class)
    public void testDivide() throws Exception {
        c.divide(0);
    }

    @Test(timeout = 1000)
    public void testSquareRoot() {
        c.squareRoot(9);
    }

}

演示结果

技术图片

 

 其中将multiply方式用ignore注解忽略到了


另外一个测试代码演示带有数据库

待测试的项目:

 1 package com.junit.test;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 import java.util.HashMap;
 9 import java.util.Map;
10 
11 public class Demo {
12     Connection conn=null;
13     public static void main(String[] args){
14         //运行之前需要创建数据库和表格,参考test1.sql文件
15         Demo d=new Demo();
16         Map param=new HashMap();
17         param.put("id",5);
18         param.put("name","amy");
19         param.put("password","123456");
20         d.insert(param);
21         d.getAll();
22         d.close();
23     }
24     public void createDb(){
25         String url="jdbc:mysql://localhost:3306/test1";
26         String name="com.mysql.jdbc.Driver";
27         String user="root";
28         String password="root";
29         try {
30             Class.forName(name);//指定连接类型
31              conn = DriverManager.getConnection(url, user,password);
32              System.out.println("连接成功");
33              //pst=conn.prepareStatement(sql);//准备执行语句
34         } catch (ClassNotFoundException e) {
35             // TODO Auto-generated catch block
36             e.printStackTrace();
37         } catch(SQLException e){
38             e.printStackTrace();
39         }
40     }
41     
42     public int insert(Map param){
43         this.createDb();
44         int i=0;
45         String sql="INSERT INTO user(id,username,password) VALUES(?,?,?)";
46         PreparedStatement pstmt;
47         try{
48             pstmt=this.conn.prepareStatement(sql);
49             pstmt.setString(1,(String)param.get("id"));
50             pstmt.setString(2,(String)param.get("name"));
51             pstmt.setString(3,(String)param.get("password"));
52             i=pstmt.executeUpdate();
53             pstmt.close();
54         }catch(SQLException e){
55             e.printStackTrace();
56         }
57         
58         return i;
59     }
60     public void getAll(){
61         String sql="select * from user";
62         PreparedStatement pstmt=null;
63         try{
64             pstmt=this.conn.prepareStatement(sql);
65             ResultSet rs=pstmt.executeQuery();
66             int col=rs.getMetaData().getColumnCount();
67             System.out.println("=========================");
68             while(rs.next()){
69                 for(int i=1;i<=col;i++){
70                     System.out.print(rs.getString(i)+"\t");
71                     if((i==2)&&(rs.getString(i).length()<8)){
72                         System.out.print("\t");
73                     }
74                 }
75                 System.out.println("");
76             }
77             System.out.println("=========================");
78             
79         }catch(Exception e){
80             e.printStackTrace();
81         }
82     }
83     public void close(){
84     }
85 }

测试代码

package com.junit.test;

import static org.junit.Assert.*;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DemoTest {
    
    public static Demo d = new Demo();
    
    @Before
    public void setUp() throws Exception {
    }

    @Before
    public void testCreateDb() {
        d.createDb();
    }

    
    @Test
    public void testInsert() {
        Map param=new HashMap();
        param.put("id","2");
        param.put("name","amy");
        param.put("password","123456");
        d.insert(param);
    }

    @Test
    public void testGetAll() {
        d.getAll();
    }

    @After
    public void testClose() {
        d.close();
    }

}

测试结果

技术图片

 

 打包所有要测试的类

package com.junit.test;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    CalculatorTest.class,
    DemoTest.class
})

public class TestAllForJunit {

}

 

Java学习十四

标签:prepare   print   throw   ted   exp   成功   date   except   指定   

原文地址:https://www.cnblogs.com/-2016/p/12245524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!