Feed4Junit官方地址:
http://databene.org/feed4junit.html
Feed4Junit测试数据来自数据库:
Feed4JUnit 1.1.2 发布了,该版本支持数据来自数据库
利用Feed4JUnit能够很方便用随机但校验过的数据执行冒烟测试来提高代码 代码覆盖率和发现由非常特殊的数据结构产生的Bug。此外还可以利用Feed4JUnit轻松定义等价类测试。
官方文档:
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: url, driver, user,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) |
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.
@Database注释在类头部:
package com.easyway.feed4junit; import static org.junit.Assert.assertEquals; import org.databene.benerator.anno.Database; import org.databene.benerator.anno.Source; import org.databene.feed4junit.Feeder; import org.junit.Test; import org.junit.runner.RunWith; import com.easyway.junit4.JunitSample.UserAccess; /* * Feed4JUnit - Get Data from Database, all test methods can use the database data */ @RunWith(Feeder.class) @Database( id = "testdb", url = "jdbc:oracle:thin:@192.168.xxx.xxx:1521:ticket", driver = "oracle.jdbc.driver.OracleDriver", user = "xxx", password = "xxx") public class F4JfromDB { @Test @Source(id = "testdb", selector = "select * from test_feed4junit") public void testAccessCheck(String userName, String pw, String expected) { Boolean bSucess = UserAccess.accessCheck(userName.trim(), pw.trim()); assertEquals(expected.trim(), bSucess.toString()); } }
@Database注释在方法:
package com.easyway.feed4junit; import static org.junit.Assert.assertEquals; import org.databene.benerator.anno.Database; import org.databene.benerator.anno.Source; import org.databene.feed4junit.Feeder; import org.junit.Test; import org.junit.runner.RunWith; import com.easyway.junit4.JunitSample.UserAccess; /* * Feed4JUnit - Get Data from Database, only the specified method can use the database data */ @RunWith(Feeder.class) public class F4JfromDB_Method { @Test @Database( id = "testdb", url = "jdbc:oracle:thin:@192.168.45.171:1521:ticket", driver = "oracle.jdbc.driver.OracleDriver", user = "tbs", password = "tbs") @Source(id = "testdb", selector = "select * from test_feed4junit") public void testAccessCheck(String userName, String pw, String expected) { Boolean bSucess = UserAccess.accessCheck(userName.trim(), pw.trim()); assertEquals(expected.trim(), bSucess.toString()); } }
@Database注释基于配置文件的方式:
在项目src的统计目录下文件必须是同级别的下文件:
dbtest.env.properties文件名称的格式为Database的注解属性中environment+“env.properties”
内容如下:
db_url=jdbc:oracle:thin:@192.168.xxxx.xxx:1521:ticket
db_driver=oracle.jdbc.driver.OracleDriver
db_user=xxxx
db_password=xxxx
测试代码:
package com.easyway.feed4junit; import static org.junit.Assert.assertEquals; import org.databene.benerator.anno.Database; import org.databene.benerator.anno.Source; import org.databene.feed4junit.Feeder; import org.junit.Test; import org.junit.runner.RunWith; import com.easyway.junit4.JunitSample.UserAccess; /* * Feed4JUnit - Get Data from Database, only the specified method can use the database data */ @RunWith(Feeder.class) public class F4JfromConfiguration { @Test @Database(id = "testdb", environment = "dbtest") @Source(id = "testdb", selector = "select * from test_feed4junit") public void testAccessCheck(String userName, String pw, String expected) { Boolean bSucess = UserAccess.accessCheck(userName.trim(), pw.trim()); assertEquals(expected.trim(), bSucess.toString()); } }