JUnit测试无法识别我的函数(通用函数)

时间:2017-10-16 21:20:05

标签: java list junit iterator generic-programming

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

}

单元测试类:

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

    }

}

所以我得到了3个类,BottleComparator实现了Comparator,Bottle实现了Comparable和exercici,这个类包含了我被要求做的方法。

问题是测试类似乎没有识别我的函数numMinors,我已经尝试将所有文​​档放在另一个包上并关闭项目并再次打开它。任何线索??

(我仍然需要将断言函数放在测试中)

错误消息:

  

找不到符号

     

symbol:方法numMinors(......)

     

location:class exerciciTest

1 个答案:

答案 0 :(得分:0)

numMinors不是您的班级ExerciciTest的静态方法。它是您班级Exercici的静态方法。

所以你可以Exercici.numMinors(...)而不是ExerciciTest.numMinors(...)

您可以直接在numMinors(...)课程中致电ExerciciTest。由于numMinors

中没有静态方法ExerciciTest,因此出现错误