Java中单元测试(Junit4和Mockito)和数据库JDBC连接示例

本文介绍如何使用Gradle进行项目配置,并通过具体示例演示如何编写和执行Java单元测试,包括Mockito和JUnit的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先添加依赖包:

保证仓库为jcenter()

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
         jcenter()
    }
}

下载jar包:
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support:recyclerview-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'mysql:mysql-connector-java:5.+'
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-all:2.+'
}

测试代码:
package app.hwb.com.hiaiframework;

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

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * @see <a href=" http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest {

    public List<String> mockedList;

    @Before
    public void Init(){
        mockedList = mock(List.class);
        System.out.println("in the Init");
    }

    @Test
    public void addition_isCorrect() throws Exception {
        //Junit单元测试
        assertEquals(4, 2 + 2);

        //创建mock对象,参数可以是类,也可以是接口
        //List<String> mockedList = mock(List.class);
        mockedList.add("one");
        //设置方法的预期返回值
        when(mockedList.get(0)).thenReturn("hhh");
        System.out.println(mockedList.size());
        verify(mockedList).size();//验证方法调用(是否调用了size())
        mockedList.clear();

        try {
            Class.forName("com.mysql.jdbc.Driver");
        }catch (Exception e){
            e.printStackTrace();
        }
        String url = "jdbc:mysql://10.12.13.88:3306/mysql";
        Connection conn;
        try{
            conn = DriverManager.getConnection(url,"root","root");

            Statement stmt = conn.createStatement();
            System.out.println("Mysql数据库连接成功!");

            String sql = "select * from user";
            ResultSet rs = stmt.executeQuery(sql);
            System.out.println("User"+"\t"+"Password");
            while (rs.next()){
                System.out.print(rs.getString(2) + "\t");
                System.out.print(rs.getString(3) + "\t");
                System.out.println();
            }
            rs.close();
            stmt.close();
            conn.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

测试结果:
in the Init
0
Mysql数据库连接成功!
User Password
root *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
root *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B

Process finished with exit code 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值