Forum Discussion

mslisamarie's avatar
mslisamarie
Occasional Contributor
3 years ago

How can I make the annotation @ParemeterType use a list?

Hello! 

As I am continuing my Cucumber 7.0.0 uplift journey I have stumbled upon some issues. The old way on how to handle parameter types is deprecated so now the annotations @ParameterType and @DataTableType are used instead. My problem is that we are using many enums (I cannot show you how the real code looks like since I signed an NDA) and before they were looped through in an Array stream. To make it work now I have to hardcode the enums in the value field like this:
@ParameterType(name = "animal_type", value "(CAT|HORSE|DOG|DONKEY|PIG)")
public Animal defineAnimalType(final String animalType) {

    return Animal.fromString(animalType);
}
And this is not an optimal solution because if the enums needs to be changed in the future people need to look up the hardcoded strings and change there as well. I read about something called @DefaultParameterTransformer and I wonder if that can be an option. 

 

Does anyone have a suggestion or solution for this problem? I would really appreciate it! 

 

Best Regards

Lisa Marie

  • NoahLucas's avatar
    NoahLucas
    New Contributor

    Hi Lisa Marie,

    I understand your problem with updating enums in Cucumber 7.0.0. Instead of hardcoding them, you can generate the enum values automatically. This way, you won't have to change the strings manually every time.

    You can use Java reflection to get all the enum values and create the pattern. Here's an example:

    @ParameterType(name = "animal_type", value = AnimalType.REGEX)
    public Animal defineAnimalType(final String animalType) {
        return Animal.fromString(animalType);
    }

    public enum AnimalType {
        CAT, HORSE, DOG, DONKEY, PIG;

        public static final String REGEX = Arrays.stream(values())
            .map(Enum::name)
            .collect(Collectors.joining("|"));
    }

    This way, any changes to the enum are automatically included in the pattern.

    For more details, visit: https://minicow.us/.