Custom Input Annotations
Custom input annotations allow particular inputs to be recommended to Diffblue Cover when writing tests.
Using @InTestsUseStrings
@InTestsUseStringsThe @InTestsUseStrings annotation allows you to recommend specific String values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated with some genuine examples of song titles that can be used to achieve coverage:
public static boolean isDayRelatedSongTitle(@InTestsUseStrings({"I Don't Like Mondays", "Here Comes The Weekend"}) String title) {
return Stream.of(DayOfWeek.values())
.map(DayOfWeek::name)
.map(String::toLowerCase)
.anyMatch(title.toLowerCase()::contains);
}Using @InTestsUseIntegers
@InTestsUseIntegersThe @InTestsUseIntegers annotation allows you to recommend specific int values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated to use a specific preferred value:
public static String toUpperHexString(@InTestsUseIntegers(0xD1FFB) int input) {
return Long.toHexString(input).toUpperCase();
}Using @InTestsUseShorts
@InTestsUseShortsThe @InTestsUseShorts annotation allows you to recommend specific short values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated to use a specific preferred value:
public static String toUpperHexString(@InTestsUseShorts((short) 0xD1FF) short input) {
return Long.toHexString(input).toUpperCase();
}Using @InTestsUseLongs
@InTestsUseLongsThe @InTestsUseLongs annotation allows you to recommend specific long values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated to use a specific preferred value:
Using @InTestsUseBytes
@InTestsUseBytesThe @InTestsUseBytes annotation allows you to recommend specific byte values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated to use a specific preferred value:
Using @InTestsUseFloats
@InTestsUseFloatsThe @InTestsUseFloats annotation allows you to recommend specific float values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated to use a specific preferred value:
Using @InTestsUseDoubles
@InTestsUseDoublesThe @InTestsUseDoubles annotation allows you to recommend specific double values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated to use a specific preferred value:
Using @InTestsUseEnums
@InTestsUseEnumsThe @InTestsUseEnums annotation allows you to recommend specific Enum values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated with the names of specific enum values preferred by the developer:
Using @InTestsUseClasses
@InTestsUseClassesThe @InTestsUseClasses annotation allows you to recommend specific Class literal values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated with an example class literal to achieve a positive test:
Using @InTestsUseCharacters
@InTestsUseCharactersThe @InTestsUseCharacters annotation allows you to recommend specific char values to use in tests. Sometimes this can be useful to control the values used for cosmetic reasons, but it can also be useful when Cover is unable to identify values to cover all cases. For example the following method is annotated with a genuine examples characters that make up a Unicode surrogate pair that can be used to achieve a positive test:
Using @InTestsUseFactories
@InTestsUseFactoriesThe @InTestsUseFactories annotation allows you to recommend specific factory methods to use in tests. Cover will try to find factory methods, but sometimes needs help. For example, the following class contains a factory for creating sports car instances.
The following method is annotated to use the sports car factory method:
Last updated
Was this helpful?

