Skip to content

Commit

Permalink
The String "null" will be always be passed as null object (#20).
Browse files Browse the repository at this point in the history
  • Loading branch information
aaschmid committed May 11, 2014
1 parent 4c492d6 commit 37f9cd0
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ class DataProviderTest {

### Let ```@DataProvider``` directly providing test data

with a fix to handling primitive wrapper classes (like Integer) for parameters passed in the annotation and the Usage Tutorial test (line below).

Instead of using ```@UseDataProvider``` to point to a method providing test data, you can directly
pass test data using ```@DataProvider``` annotation and its ```#value()``` method to provide an
array of comma-separated ```String```s. Each comma-separated ```String``` is split and trimmed back by
Expand All @@ -235,6 +233,8 @@ resulting ```String``` is then parsed to its corresponding type in the test meth
types (e.g. ```char```, ```boolean```, ```int```), primitive wrapper types (e.g. ```Long```, ```Double```), ```Enum```s,
and ```String```s are supported.

*Note:* The ```String``` "null" will always be passed as ```null```.

```java
// @formatter:off
@Test
Expand All @@ -249,8 +249,24 @@ and ```String```s are supported.
// Expect:
assertThat(str.length()).isEqualTo(expectedLength);
}

@Test
@DataProvider({
"null",
"",
})
public void testIsEmptyString2(String str) {
// When:
boolean isEmpty = (str == null) ? true : str.isEmpty();

// Then:
assertThat(isEmpty).isTrue();
}
```

Further examples can be found in [DataProviderJavaAcceptanceTest.java](/src/test/java/com/tngtech/test/java/junit/dataprovider/DataProviderJavaAcceptanceTest.java).


Release notes
-------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* {@code double}), primitive wrapper types (e.g. {@link Boolean}, {@link Long}), case-sensitive {@link Enum} values, or
* {@link String}s. The former two are converted using the {@code valueOf(String)} methods of their corresponding
* wrapper classes or {@code valueOf(Class<? extends Enum<?>>, String)}, respectively. This can cause {@link Exception}s
* at runtime. Latter must not contain commas!</li>
* at runtime. Latter must not contain commas! The {@link String} "null" will always be passed as {@code null}.</li>
* </ul>
* <p>
* Copyright by TNG Technology Consulting GmbH, Germany
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ Object[] getParameters(String data, Class<?>[] parameterTypes, int rowIdx) {

private Object convert(String str, Class<?> targetType) {

if ("null".equals(str)) {
return null;

This comment has been minimized.

Copy link
@janschaefer

janschaefer May 11, 2014

ok, that was an easy one ;-)

This comment has been minimized.

Copy link
@aaschmid

aaschmid May 11, 2014

Author Member

And honestly, I though you would say that, therefore I have already built it before my commen :P

This comment has been minimized.

Copy link
@janschaefer

janschaefer May 11, 2014

hehe

}

if (String.class.equals(targetType)) {
return str;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ public void testGetParametersShouldCorrectlyParseAllPrimitiveWrapperTypes() {
String data = "true,1,c,2,3,4,5.5,6.6";
Class<?>[] parameterTypes = new Class[] { Boolean.class, Byte.class, Character.class, Short.class,
Integer.class, Long.class, Float.class, Double.class };
int rowIdx = 1;
int rowIdx = 12;

// When:
Object[] result = underTest.getParameters(data, parameterTypes, rowIdx);
Expand All @@ -1080,6 +1080,20 @@ public void testGetParametersShouldCorrectlyParseAllPrimitiveWrapperTypes() {
Integer.valueOf(3), Long.valueOf(4L), Float.valueOf(5.5f), Double.valueOf(6.6d) });
}

@Test
public void testGetParametersShouldCorrectlyParseNullValue() {
// Given:
String data = "null, null ";
Class<?>[] parameterTypes = new Class[] { Boolean.class, String.class };
int rowIdx = 13;

// When:
Object[] result = underTest.getParameters(data, parameterTypes, rowIdx);

// Then:
assertThat(result).isEqualTo(new Object[] { null, null });
}

private Method getMethod(String methodName, Class<?>... args) {
final Class<? extends DataProviderRunnerTest> clazz = this.getClass();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,17 @@ public void testOldModeToRoundingMode(int oldMode, RoundingMode expected) {
// Then:
assertThat(result).isEqualTo(expected);
}

@Test
@DataProvider({
"null",
"",
})
public void testIsEmptyString2(String str) {
// When:
boolean isEmpty = (str == null) ? true : str.isEmpty();

// Then:
assertThat(isEmpty).isTrue();
}
}

0 comments on commit 37f9cd0

Please sign in to comment.