0

When I run a java parameterized class:

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);
    }

}


I get the following error:

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [int arg0] in constructor [public ParametrizedFillTest(int,int)].

My FuelTank class is the following:

/**
 * 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;
        }
}

How do I solve this problem?

1 Answer 1

1

Please consider using the Parameterized Tests of Junit5. In this example using the CsvSource is an easy solution: link

It would look like:

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");
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.