Feed4Junit官方地址:
http://databene.org/feed4junit.html
官方文档:
Special CSV and Excel(TM) Sheet Features
Feed4JUnit supports the import of CSV files and Excel(TM) Sheets, formulas in Excel Sheets are resolved. In the following chapters, the term data file represents CSV files as well as Excel files.
Data file per method
When annotating a test method with a @Source that refers to a data file, each column is mapped to a method parameter sequentially, independent of the column name and the parameter name:
@Test |
The corresponding data file is expected to have a header row and an arbitrary number of rows. Each row represents one data set with which the test method is called. If the file users.ent.csv has the following content:
name,age Alice,23 Bob,34 |
the test method is called twice: First with the parameters (Alice, 23), then with the parameters (Bob, 34).
CSV format
Feed4JUnit supports CSV as specified in RFC 4180. Thus, cells may be double-quoted, e.g. to include the separator character as plain character. As an example, the line
Alice,"Alice","Bob,Charly"
is parsed into a data set of three string values, which the first two entries contain the string Alice and the third one the string Bob,Charly. Note that each one is provided without quotes.
For differing between empty strings and null values, empty double quotes are mapped to a string, an empty cell is mapped to a null value:
Alice,,""
is mapped to a data set of three values, the first one is the string Alice, the second one null, the third one an empty string.
Excel(TM) format
Excel sheets are imported using the Apache POI library which provides any numerical value as a floating point value. So be prepared that some fractional numbers are not parsed exactly as you expect them to be.
Empty cells are mapped to null values, The default notation for an empty string in Excel is one single-quote character in a cell. Unfortunately this is not displayed in the sheet view, so the user needs to enter each cell in order to tell if its value represents null or an empty string. If the difference between null and empty strings matters for you, Excel sheets would become much more intuitive if each visually empty cell is indeed empty and mapped to a null value and each cell that represents an empty string, displays a marker text. This approach can be used by Feed4JUnit's emptyMarker.
The user can chose a custom marker text to represent empty strings, e.g. <empty>, and use it in an Excel sheet:
Then the emptyMarker needs to be declared in Feed4JUnit's @Source annotation:
@Test |
Finally, when running the tests, the values which match the emptyMarker are replaced with empty strings. The example yields the data sets:
1. Alice and an empty string
2. Bob an a null value
Column-based data
For tests with extraordinarily long parameter lists and few test cases to execute, a column-based data format might be more convenient for the user. In this case, the first column is expected to be the header column and each further column represents a parameter set for a test method invocation. The data of the example above would be represented like this:
name,Alice,Bob age,23,34 |
and the @Source annotation needs a flag rowBased = false:
@Test @Source(uri = "user-columns.ent.csv", rowBased = false) public void testMethodColumnSource(String name, int age) { ... } |
JavaBean parameters
Data for a JavaBean parameter can be read from a data file. Suppose you have a JavaBean class Country with default constructor and the properties 'isoCode' and 'name':
public class Country { private String isoCode; private String name; public String getIsoCode() { return isoCode; } public void setIsoCode(String isoCode) { this.isoCode = isoCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Then you can annotate a method's JavaBean-type-parameter with a @Source annotation that points to the file:
@Test public void testCsvBean(@Source("countries.ent.csv") Country country) { ... } |
In this case, the column names of the data file matter and are mapped to the bean's properties of same name.
isoCode,name |
Reading JavaBean graphs from a data file
Bean graphs can also be imported from a single data file. Suppose you have a 'User' JavaBean which references the 'Country' JavaBean:
public class User { private String name; private int age; private Country country; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Country getCountry() { return country; } public void setCountry(Country country) { this.country = country; } } |
Feed4JUnit recognizes that the User's 'country' property is a JavaBean and when importing file data for a User
@Test public void testCsvNestedBean(@Source("users2.ent.csv") User user) { ... } |
it maps the data appropriately based on recursive property naming:
name,age,country.isoCode,country.name Alice,23,DE,Germany Bob,34,US,USA |
Locating data files
Feed4JUnit uses the following default strategy for looking up file names specified in a @Source annotation's uri:
- absolute (if it is an absolute path like C:\tests\my.csv or /tests/my.csv)
- class path
- relative to the working directory
For alternative lookup strategies, a service provider interface is supported: org.databene.benerator.anno.PathResolver.
By default, Feed4JUnit uses the org.databene.benerator.anno.DefaultPathResolver which employs the behaviour described above.
An alternative strategy is provided by org.databene.benerator.anno.RelativePathResolver: It uses a configurable base path, adds the package name of the test to run (as path components) and finally adds the resource path specified as URI. So, when using a base path C:\tests, an annotation @Source("p1/user.ent.csv") used in test class com.my.UserTests is mapped to the file path C:\tests\com\my\p1\user.ent.csv.
If you need alternative strategies, you can write a custom implementation of the PathResolver interface.
For choosing a specific path resolver, put a file feed4junit.properties into your project (or working) directory and specify a pathResolver:
pathResolver=new org.databene.benerator.anno.RelativePathResolver('C:\\test') |
The example shows how to configure the RelativePathResolver using the base path C:\test.
The pathResover expression is interpreted using DatabeneScript which supports most Java expression types. A main difference is, that strings literals use single quotes as shown in the example. Also note that string values are interpreted Java-like, so the Windows path separator \ is represented by its Java escape character.Â
package com.easyway.feed4junit; public class Country { private String isoCode; private String name; public String getIsoCode() { return isoCode; } public void setIsoCode(String isoCode) { this.isoCode = isoCode; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.easyway.feed4junit; public class User { private String name; private int age; private Country country; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Country getCountry() { return country; } public void setCountry(Country country) { this.country = country; } }
package com.easyway.feed4junit; import org.databene.benerator.anno.Source; import org.databene.feed4junit.Feeder; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(Feeder.class) public class F4JreadComplexJavaBean { @Test public void testCsvNestedBean(@Source("complex-data.csv") User user) { System.out.println(user.getName()+" "+user.getAge() +" "+user.getCountry().getIsoCode() +" "+user.getCountry().getName()); } }
相关推荐
此外,Feed4JUnit还支持多种数据格式,适应不同的项目需求。 2. 单元测试的重要性 单元测试能够确保代码在方法级别上的正确性,同时有助于发现设计上的问题。编写单元测试迫使开发者思考代码的边界条件和异常情况...
2. **创建测试注解**:在JUnit测试类或测试方法上,使用Feed4JUnit提供的注解(如`@RunWith(Feed4junit.class)`和`@DataFile`)来指定数据文件和如何解析数据。 3. **数据绑定**:通过注解指定如何将文件中的数据...
源代码分析还能让我们了解feed4testng是如何处理测试数据的,例如是否支持XML配置文件导入,或者是否提供了自定义数据提供器。 "feed4testng-1.0"这个文件名可能是指该版本是feed4testng的第一个正式发布版本,通常...
【标题】"easy-feed-oven-main-源码.rar" 是一个包含源代码的压缩文件,很可能是某个软件或系统的主程序部分。从名称上看,"easy-feed-oven" 可能是一个与自动化喂养或者烹饪设备相关的项目,而 "main" 指的是这个...
4. **GUI(图形用户界面)设计**:如果这是一个桌面应用,可能会使用JavaFX或Swing库来创建用户界面,显示RSS feed的标题、摘要和链接等信息。 5. **线程处理**:为了异步加载和更新RSS feed,可能使用Java的线程或...
Android提供AlarmManager服务来安排周期性的任务,或者使用JobScheduler API在满足特定条件时执行任务。开发者可能还使用了WorkManager,这是一个更现代且灵活的后台任务管理库,以确保即使在后台限制严格的环境中也...
5. **JSON解析**:播客数据通常以JSON格式存储,因此需要使用Gson或Jackson库来解析和序列化JSON数据。 6. **用户界面**:界面设计可能使用XML布局文件定义,包括ListView或RecyclerView显示播客列表,播放控制界面...
3. **XML或RSS解析**: 许多博客提供RSS(Really Simple Syndication)或Atom格式的feed,允许用户订阅更新。Java中的`javax.xml.parsers`包可用于解析这些XML文件,提取出文章标题、作者、发布日期和内容。 4. **...
9. **文件I/O和数据库操作**:为了持久化数据,项目可能包含了读写文件或与数据库交互的代码。这可能涉及到`java.io`和`java.sql`包中的类。 10. **测试**:良好的项目应该包含单元测试和集成测试,以验证各个组件...