1

есть класс, который считает корни квадратного уравнения, нужно написать параметризованный тест как правильно передать параметры в тест из метода getArguments? выдает такую ошибку:

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [double arg0] in constructor [public com.epam.rd.autotasks.QuadraticEquationNoRootsCasesTesting(double,double,double)].

QuadraticEquation.java

public class QuadraticEquation {
    public String solve(double a, double b, double c) {
        if(a == 0)
            throw new IllegalArgumentException();
        double D = b * b - (4 * a * c);
        if(D < 0) {
            return "no roots";
        } else if(D == 0) {
            return Double.toString(b / 2 / a * (-1));
        } else {
            double x1 = (((-1) * b + Math.sqrt(D)) / (2 * a));
            double x2 = (((-1) * b - Math.sqrt(D)) / (2 * a));
            return Double.toString(x1) + " " + Double.toString(x2);
        }
    }
}

QuadraticEquationNoRootsCasesTesting.java

import org.junit.jupiter.params.provider.Arguments;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

@RunWith(Parameterized.class)
public class QuadraticEquationNoRootsCasesTesting {

    protected QuadraticEquation quadraticEquation = new QuadraticEquation();

    private double a;
    private double b;
    private double c;

    public QuadraticEquationNoRootsCasesTesting(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @ParameterizedTest
    @MethodSource("getArguments")
    public void testNoRootsCase(double a, double b, double c) {
        System.out.println(quadraticEquation.solve(a, b, c));
    }
    static Stream<Arguments> getArguments (){
        return Stream.of(
                Arguments.of(1.0, 0.0, 0.0),
                Arguments.of(2.0, 0.0, 0.0),
                Arguments.of(3.0, 0.0, 0.0),
                Arguments.of(4.0, 0.0, 0.0));
    }
}

0

1 ответ 1

0

Простым решением будет убрать параметры из аргументов конструктора QuadraticEquationNoRootsCasesTesting, конструктор в вашем случае можно просто удалить, так как он ничего не делает.

Параметры теста передаются через аргуметны тестового метода public void testNoRootsCase(double a, double b, double c).

1
  • все равно остается ошибка No ParameterResolver registered for parameter [double arg0] in constructor 4 авг 2022 в 14:57

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.