zoukankan      html  css  js  c++  java
  • Java学习十四

    学习内容:

    1.Junit

    2.maven安装配置环境


    一、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)+"	");
    71                     if((i==2)&&(rs.getString(i).length()<8)){
    72                         System.out.print("	");
    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 {
    
    }

     maven

    之前已经安装配置过maven,今天算是重温一下。

    maven的几个命令

    mvn compile   编译

    mvn install       编译,运行和发布

    mvn package   编译,运行不负责发布,也就是package命令不会将target内容发布到本地仓库

    mvn test   编译,如果你test包下面有相关Java也编译和运行,但是也一样不会发布到本地仓库中

    mvn clean 清除

     明天就是2.1了,年也过了,不管考研结果怎么样,也要好好准备了。

    从明天起,每天6小时学习时间(其实也不多~)一点点加吧

    还有背单词!奥利给~

  • 相关阅读:
    CountDownLatch、CyclicBarrier、Semaphore的区别
    Java最小堆解决TopK问题
    【转载】缓存穿透,缓存击穿,缓存雪崩解决方案分析
    缓存雪崩问题及处理方案
    memcached 缓存数据库应用实践
    Redis分布式锁的正确实现方式
    JVM垃圾回收--年轻代、年老点和持久代(转)
    策略模式—Java实现(转)
    (转) Redis哨兵的详解
    Hibernate中1+N问题以及解决方法
  • 原文地址:https://www.cnblogs.com/-2016/p/12245524.html
Copyright © 2011-2022 走看看