JUnit 5 @ParameterizedTest with using class as @ValueSource [duplicate]
Asked Answered
A

1

11

I want to do a ParameterizedTest with @ValueSource , i have readed many tutorials including Guide to JUnit 5 Parameterized Tests:

/**
 * The {@link Class} values to use as sources of arguments; must not be empty.
 *
 * @since 5.1
 */
Class<?>[] classes() default {};

So i tried the following :

@ParameterizedTest
@ValueSource(classes = {new Mandator("0052", "123456", 79)})
void testCalculateMandateI(Mandator value) {

     //code using Mandator here
}

Mandator class:

 public class Mandator {

    private String creditorPrefix;
    private String retailerCode;
    private int mandateIdSequence;

    public Mandator(final String creditorPrefix, final String retailerCode, final int mandateIdSequence) {
        this.creditorPrefix = creditorPrefix;
        this.retailerCode = retailerCode;
        this.mandateIdSequence = mandateIdSequence;
    }

    public String getCreditorPrefix() {
        return creditorPrefix;
    }

    public String getRetailerCode() {
        return retailerCode;
    }

    public int getMandateIdSequence() {
        return mandateIdSequence;
    }
}

But i get the following error from IntelliJ , hovering above @ValueSource :

enter image description here

Attribute value must be a constant

What am i doing wrong here ? What am i missing ?

Adlai answered 24/6, 2019 at 12:42 Comment(2)
The first one explains how to use JUnit with sources. The others explain the cause of the error, "Attribute value must be a constant", you received. They're all relevant.Monoatomic
if you ever need test against class definition @ParameterizedTest @ValueSource(classes = {ClassA.class, ClassB.class, ClassC.class}) void classTest(Class<?> clazz) { assertThat( /* Logic related with class check */).isTrue(); }Octopus
S
25

Its not about JUnit but about Java Syntax.

Its impossible to create new objects in parameters of the annotations.

The type of an annotation element is one of the following:

  • List A primitive type (int, short, long, byte, char, double, float, or boolean)
  • String
  • Class (with an optional type parameter such as Class)
  • An enum type
  • An annotation type
  • An array of the preceding types (an array of arrays is not a legal element type)

If you want to supply values out of created objects consider using something like this as one possible solution:

@ParameterizedTest
@MethodSource("generator")
void testCalculateMandateI(Mandator value, boolean expected)

// and then somewhere in this test class  
private static Stream<Arguments> generator() {

 return Stream.of(
   Arguments.of(new Mandator(..), true),
   Arguments.of(new Mandator(..), false));
}
Stillness answered 24/6, 2019 at 12:57 Comment(3)
Excellent , i just don't understand what is that true, false you have added on Arguments.of()Adlai
It should be an expected result. After all given different parameters you expect different results to be verified. It doesn't have to be a boolean though, only something that you will be able to verify upon.Stillness
a list of random parameters is the only useful feature of ParameterizedTest, on my opinion. I am surprised it is not included in basic ValueSource, like list of Object[]. thanks for example!Maxantia

© 2022 - 2024 — McMap. All rights reserved.