What's new in Feed4JUnit 1.1.2
Empty strings and null values in CSV files
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.
Empty strings and null values in Excel(TM) files
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 in CSV files and Excel sheets
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.


