正在运行的Java参数化类给出错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正在运行的Java参数化类给出错误相关的知识,希望对你有一定的参考价值。

当我运行java参数化的类时:

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.Collection;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
class ParametrizedFillTest {
    FuelTank tank = null;

    private int param;
    private int result;

    @Parameters
    public static Collection<Object[]> fillTank() {
      Object[][] numbers = new Object[][] {{10,20},{15,35},{20,30},{35,45}};
      return Arrays.asList(numbers);
    }

    public ParametrizedFillTest(int param, int result) {
        this.param = param;
        this.result = result;
      }

    @BeforeEach
    void setUp() throws Exception {
        tank = new FuelTank(60,10);
    }

    @Test
    void testFill() {
        tank.fill(this.param);
        System.out.println("Parameterized param is : " + param);
        assertEquals(this.result, tank.getTankLevel(), 0);
    }

}


我收到以下错误:

org.junit.jupiter.api.extension.ParameterResolutionException:否在构造函数中为参数[int arg0]注册的ParameterResolver[public ParametrizedFillTest(int,int)]。

我的FuelTank类如下:

/**
 * FuelTank is the class which represents the fuel tank of a car.
 * A FuelTank object encapsulates the state information needed for describing the state of the tank:
 * <ul>
 *   <li> tankMax   capacity of the tank
 *   <li> tankLevel fuel level of the tank
 * </ul>
 * 
 * class invariant      0.0 &lt;= tankLevel &lt;= tankMax
 * 
 * @author UC3M MOOC Team
 *
 */

public class FuelTank {
    private double tankLevel;
    private double tankMax;

       /**
        * FuelTank is a constructor of the class. 
        * 
        * <hr>
        * <br> precondition  tankMax &gt; 0.0 and 0.0 &lt;= tankLevel &lt;= getTankMax()  
        * <br> postcondition tankMax &gt; 0.0 and 0.0 &lt;= tankLevel &lt;= getTankMax() 
        * <hr>
        * 
        * @param tankMax  is the amount of fuel  (measured in liters) that the tank can hold
        * @param tankLevel is the amount of fuel (measured in liters) that the tank will have initially
        * 
        */ 
        FuelTank(double tankMax, double tankLevel) {
           this.tankMax   = tankMax;
           this.tankLevel = tankLevel;
        }

       /**
        * getTankLevel is an accessor method
        * 
        * @return   the amount of fuel in the tank
        */
        public double getTankLevel(){
           return tankLevel;
        }

       /**
        * getTankMax is an accessor method
        * 
        * @return   the capacity (in liters) of the tank
        */
        public double getTankMax(){
           return tankMax;
        }

       /**
        * isEmpty gives a status report 
        * 
        * @return   <code>true</code> if the tank is empty 
        *          <code>false</code> otherwise.
        */
        public boolean isEmpty(){
          return tankLevel == 0;
        }

        /**
         * isFull gives a status report 
         * 
         * @return  <code>true</code> if the tank is full 
         *          <code>false</code> otherwise.
         */
        public boolean isFull(){
          return tankLevel == tankMax;
        }

       /**
        * fill is a mutator method that adds fuel to the tank
        * 
        * <hr>
        * <br> precondition     0.0 &lt; amount &lt;= getTankMax() - getTankLevel() 
        * <br> postcondition    not empty
        * <br> postcondition    tankLevel &gt; tankLevel_initial 
        * <hr>
        * 
        * @param amount     the quantity of fuel to add
        * 
        */
        public void fill(double amount){
           tankLevel = tankLevel + amount;
        }

       /**
        * consume is a mutator that consumes amount of fuel
        * 
        * @param amount the amount of fuel to consume
        * 
        */
        public void consume(double amount){
           tankLevel = tankLevel - amount;
        }
}

我该如何解决这个问题?

答案

请考虑使用Junit5的参数化测试。在此示例中,使用CsvSource是一个简单的解决方案:link

它看起来像:

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class FuelTankTest {

    FuelTank tank;

    @BeforeEach
    void setUp() throws Exception {
        tank = new FuelTank(60, 10);
    }

//The csv source translates to the arguments in 
//void test(double amount, double expectedTankLevel) like following:
//"elm1, elm2" -> double amount = elm1 and double expectedTankLevel = elm2
    @ParameterizedTest
    @CsvSource({ "10,20", "20,30", "30.4711,40.4711" }) 
    void test(double amount, double expectedTankLevel) {
        tank.fill(amount);
        assertEquals(expectedTankLevel, tank.getTankLevel(), "There is a leak in tank");
    }

}

以上是关于正在运行的Java参数化类给出错误的主要内容,如果未能解决你的问题,请参考以下文章

运行Servlet时报实例化类错误,再次刷新变404

参数化类和元类有啥区别(请使用 Python 中的代码示例)?

无法在以前工作的脚本上实例化类

实例化类的 Python3 问题

无法使用 Hibernate createQuery 实例化类

在Java中获取参数化类的实例[重复]