Junit参数化测试方法(四)

       本文将描述Feed4Junit测试数据来自数据库的情况:

一、Feed4Junit官方地址:

http://databene.org/feed4junit.html

Retrieving test data from a database

Databases can be declared and configured with a @Database annotation. When applying the annotation to a class, the defined database is available to all test methods. When annotating a method, the database is available only in this method. A basic configuration can be performed by specifying the typical JDBC connection settings: urldriveruser,password. The database declaration must specify an id, by which the database can be referred to as @Source of a method. The @Source annotation must refer to the database idand define a SQL query as selector. The number of query result columns must match the number of method parameters:

@RunWith(Feeder.class)
@Database(id = "db", url = "jdbc:hsqldb:hsql://localhost:9001/f4jdb", 
        driver = "org.hsqldb.jdbcDriver", user = "me", password = "secret")
public class DatabaseTest {  
    static DBSystem db;
    
    @Test
    @Source(id = "db", selector = "select id, name from dbt_person")
    public void test(int id, String name) {
        System.out.println(id + ", " + name);
    }
    
}

Alternatively to the explicit database connection configuration, you can place database environment configurations in your user directory and refer to them in the test:

@Database(id = "db", environment = "f4jdb") 

Read more about environment files in the DB Sanity environment files documentation.

Environment Files 

An environment file carries the identifier of the environment in its name: An environment 'mydb' is configured in a properties file 'mydb.env.properties'. The environment name is the only required argument for command line execution of DB Sanity.

The related properties file first is searched in the current working directory. If it is not found there, DB Sanity looks up the environment in a central configuration directory below your user's home directory: $USER.HOME/databene/. This way you do not need to copy environment files for each DBSanity project you are using and can reuse them in other Databene applications like Benerator. You can as well specify an environment name that refers to another directory. For example, an environment name 'config/test' refers to a file'config/test.env.properties'.

In the file, you need to specify details for connecting your database with a JDBC driver:

Name Description  
db_url JDBC URL of the database required 
db_driver Java class name of the JDBC driver required 
db_user user name for database login optional 
db_password password for database login optional 
db_catalog the (JDBC) catalog to use optional 
db_schema the (JDBC) schema to use optional 


If you are not familiar with JDBC, you can look up driver archive names, class names and url formats in a table in the Benerator documentation: http://databene.org/databene-benerator/116-my-first-ide-and-maven-based-benerator-project.html.

二、示例

1、@Database注释在类头部:

 1 package JunitTestSample0001;
 2 
 3 import org.databene.benerator.anno.Database;
 4 import org.databene.benerator.anno.Source;
 5 import org.databene.feed4junit.Feeder;
 6 import org.junit.Assert;
 7 import org.junit.Test;
 8 import org.junit.runner.RunWith;
 9 @RunWith(Feeder.class)
10 @Database(
11         id = "DEEJdb",   
12         url = "jdbc:oracle:thin:@zs-PC:1521:ytdf",   
13         driver = "oracle.jdbc.driver.OracleDriver",   
14         user = "deejuser",   
15         password = "deejuser") 
16 public class JunitTest0001 {
17     @Test
18     @Source(id = "DEEJdb", selector = "select packagsn from product")
19     public void test(String packagsn, String resultInfo) {
20         System.out.println("-------------");
21         Assert.assertEquals(packagsn, packagsn);
22     }
23 }
View Code

运行失败,空指针异常:

 

2、@Database注释在方法:

 1 package JunitTestSample0001;
 2 
 3 import org.databene.benerator.anno.Database;
 4 import org.databene.benerator.anno.Source;
 5 import org.databene.feed4junit.Feeder;
 6 import org.junit.Assert;
 7 import org.junit.Test;
 8 import org.junit.runner.RunWith;
 9 
10 @RunWith(Feeder.class)
11 @Database(id = "DEEJdb", environment = "DEEJdb")  
12 public class JunitTest0001 {
13     @Test
14     @Source(id = "DEEJdb", selector = "select packagsn from product")
15     public void test(String packagsn, String resultInfo) {
16         System.out.println("-------------");
17         Assert.assertEquals(packagsn, packagsn);
18     }
19 }
View Code

运行失败,空指针异常:

 

3、@Database注释基于配置文件的方式:

1)文件名称:DEEJdb.env.properties

    格式:Database的注解属性中environment+“env.properties”

    内容如下:

db_url=jdbc:oracle:thin:@zs-PC:1521:ytdf
db_driver=oracle.jdbc.driver.OracleDriver
db_user=deejuser
db_password=deejuser

2)放置在在项目src的统计目录下(文件必须是同级别的下文件)

 

转载于:https://www.cnblogs.com/shuaijie/articles/5833339.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值