Home > Net >  JUnit Test - ParameterizedTests - No implicit conversion to convert object of type Lab_4.Point to ty
JUnit Test - ParameterizedTests - No implicit conversion to convert object of type Lab_4.Point to ty

Time:09-29

I'm using JUnit 5. Think that i'm missing something:\

class KPointTest {
@ParameterizedTest
@MethodSource("generateArgument")
void testForPoint(ArrayList<Point> points)
{
    KPoint k = new KPoint(10);
    for(Point po: points)
    {
        assertTrue(1 == k.point(po));
    }

}
public static Stream<Arguments> generateArgument()
{
    ArrayList<Arguments> p = new ArrayList<Arguments>();

    p.add(Arguments.of(new Point(1,2)));
    p.add(Arguments.of(new Point(3,7)));
    p.add(Arguments.of(new Point(5,8)));
    p.add(Arguments.of(new Point(4,8)));
    return p.stream();
}

Error:

org.junit.jupiter.api.extension.ParameterResolutionException: Error converting parameter at index 0: No implicit conversion to convert object of type Lab_4.Point to type java.util.ArrayList

CodePudding user response:

Try this:

class KPointTest {
@ParameterizedTest
@MethodSource("generateArgument")
void testForPoint(Point point)
{
    KPoint k = new KPoint(10);

    assertTrue(1 == k.point(po));
}
public static Stream<Arguments> generateArgument()
{
    ArrayList<Arguments> p = new ArrayList<Arguments>();

    p.add(Arguments.of(new Point(1,2)));
    p.add(Arguments.of(new Point(3,7)));
    p.add(Arguments.of(new Point(5,8)));
    p.add(Arguments.of(new Point(4,8)));
    return p.stream();
}

CodePudding user response:

Write your test as

class KPointTest {
@ParameterizedTest
@MethodSource
void testForPoint(Point point)
{
    KPoint k = new KPoint(10);
    for(Point po: points)
    {
        assertTrue(1 == k.point(po));
    }

}
 static Stream<Arguments> testForPoint()
{
   return Stream.of(Arguments.of(new Point(1,2)),
    Arguments.of(new Point(3,7)),
    Arguments.of(new Point(5,8)),
    Arguments.of(new Point(4,8)));
}

Your test method recieves only a single item of the Stream, therefore the test method need according argument.

You could declare multiple arguments for your test method like this

@ParameterizedTest
    @MethodSource
    void testForPoint(Point point, String someValue) {... }
static Stream<Arguments> testForPoint()
    {
       return Stream.of(Arguments.of(new Point(1,2), "abc"),
        Arguments.of(new Point(3,7), "def"),
        Arguments.of(new Point(5,8), "ghi"),
        Arguments.of(new Point(4,8), "jkl"));
    }
  • Related