@NullSource, @EmptySource and @NullAndEmptySource annotations are used to supply null and empty values to our parameterized tests.
Below table summarizes above annotations.
| 
   Annotation  | 
  
   Description  | 
 
| 
   @NullSource  | 
  
   Provides a single null argument to the annotated @ParameterizedTest method. It cannot be used for a parameter that has a primitive type. 
 @NullSource cannot be used for a parameter that has a primitive type.  | 
 
| 
   @EmptySource  | 
  
   provides a single empty argument to the annotated @ParameterizedTest method for parameters of the following types: java.lang.String, java.util.List, java.util.Set, java.util.Map, primitive arrays (ex: int[], char[][], etc.), object arrays (ex: String[], Integer[][], etc.).  | 
 
| 
   @NullAndEmptySource  | 
  
   Composed annotation that combines the functionality of @NullSource and @EmptySource.  | 
 
Example
@ParameterizedTest
@NullAndEmptySource
public void getNumberOfElementsInList_nullAndemptyList(List<Integer> input) {
	int actualResult = getNumberOfElementsInList(input);
	if (input == null) {
		assertEquals(-1, actualResult);
	} else {
		assertEquals(0, actualResult);
	}
}
Find the below working application.
NullAndEmptySourceTest.java
package com.sample.app;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EmptySource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.NullSource;
public class NullAndEmptySourceTest {
	private static int getNumberOfElementsInList(List<Integer> list) {
		if (list == null) {
			return -1;
		}
		return list.size();
	}
	@ParameterizedTest
	@NullSource
	public void getNumberOfElementsInList_null_negativeOne(List<Integer> input) {
		int actualResult = getNumberOfElementsInList(input);
		assertEquals(-1, actualResult);
	}
	@ParameterizedTest
	@EmptySource
	public void getNumberOfElementsInList_emptyList_negativeOne(List<Integer> input) {
		int actualResult = getNumberOfElementsInList(input);
		assertEquals(0, actualResult);
	}
	@ParameterizedTest
	@NullAndEmptySource
	public void getNumberOfElementsInList_nullAndemptyList(List<Integer> input) {
		int actualResult = getNumberOfElementsInList(input);
		if (input == null) {
			assertEquals(-1, actualResult);
		} else {
			assertEquals(0, actualResult);
		}
	}
}
Run above application, you will see the test result in junit window.
You can also combine @NullSource, @EmptySource, and @ValueSource to test a wider range of null, empty, and blank input.
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = { " ", "   ", "\t", "\n", "   \t  " })
void isStringEmptyOrNull_emptyNullCheck(String input) {
	boolean actual = isStringEmptyOrNull(input);
	assertTrue(actual);
}
ParameterizedTest2.java
package com.sample.app;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
public class ParameterizedTest2 {
	private static boolean isStringEmptyOrNull(String str) {
		return str == null || str.trim().isEmpty();
	}
	@ParameterizedTest
	@NullAndEmptySource
	@ValueSource(strings = { " ", "   ", "\t", "\n", "   \t  " })
	void isStringEmptyOrNull_emptyNullCheck(String input) {
		boolean actual = isStringEmptyOrNull(input);
		assertTrue(actual);
	}
}
Previous Next Home


No comments:
Post a Comment