Search code examples
javalistjunititeratorgeneric-programming

JUnit test doesn't recognise my function (Generic functions)


package Comparar;

import java.util.*;

public class Exercici {
    
    public void Exercici(){
        
    }
    
    public static <E extends Comparable <E>> int numMinors( E e, Iterator<E> it){
        int numMenors = 0;
        while(it.hasNext()){
            int comparacio = e.compareTo(it.next());
            if (comparacio == -1){
                numMenors += 1;
            }
        }
        return numMenors;
    }
    
    public static <E extends Comparable <E>> int numMinors(Comparator<E> comp, E e, Iterator<E> it){
        int numMenors =0;
        while (it.hasNext()){
            int comparacio = comp.compare(e, it.next());
            if(comparacio == -1){
                numMenors += 1;
            }
        }
        return numMenors;
    }

}

The Unit test class:

import java.util.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

public class ExerciciTest {

    public ExerciciTest() {

    }

    @Test
    public void compararCosesComparables() {
        int num = 0;

        Bottle b1 = new Bottle(0.33, 10, "Agua de Oro", "Dubai");
        Bottle b2 = new Bottle(1.5, 1, "asdf", "Rabat");
        Bottle b3 = new Bottle(2.5, 1.5, "qwerty", "Londres");
        Bottle b4 = new Bottle(5, 2, "poiuy", "Paris");
        Bottle b5 = new Bottle(2.5, 2, "Botella", "EEUU");

        List<Bottle> alb = new ArrayList<>();
        Iterator<Bottle> it = alb.iterator();
        alb.add(b1);
        alb.add(b2);
        alb.add(b3);
        alb.add(b4);

        num = numMinors(b5, it); //Error cannot find symbol

    }

    @Test
    public void compararCosesAmbComparador() {
        int num = 0;

        Bottle b1 = new Bottle(0.33, 10, "Gold water", "Dubai");
        Bottle b2 = new Bottle(1.5, 1, "asdf", "Rabat");
        Bottle b3 = new Bottle(2.5, 1.5, "qwerty", "Londres");
        Bottle b4 = new Bottle(5, 2, "poiuy", "Paris");
        Bottle b5 = new Bottle(2.5, 2, "Botella", "EEUU");

        ArrayList<Bottle> alb = new ArrayList<>();
        BottleComparator comp = new BottleComparator();
        Iterator<Bottle> it = alb.iterator();

        alb.add(b1);
        alb.add(b2);
        alb.add(b3);
        alb.add(b4);

        num = numMinors(comp, b5, it); //Error cannot find symbol

    }

}

So I got 3 classes, BottleComparator which implements Comparator, Bottle which implements Comparable and exercici which is the class that contains the methods i got asked to do.

The problem is that the test class seems not to recognise my function numMinors, i already tried to put all the docs on another package and to close the project and open it another time. Any clue??

(I still need to put the assert functions on the test)

The error message:

Cannot find symbol

symbol: method numMinors (......)

location: class exerciciTest


Solution

  • numMinors isn't a static method of your class ExerciciTest. It's a static method of your class Exercici.

    So you can do Exercici.numMinors(...) but not ExerciciTest.numMinors(...).

    Here you call numMinors(...) directly in your ExerciciTest class. There is an error since there is no static method numMinors in ExerciciTest class