JUnit 测试无法识别我的函数(通用函数)
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;
}
}
单元测试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
}
}
所以我得到了 3 个 classes,实现 Comparator 的 BottleComparator,实现 Comparable 的 Bottle,以及包含我被要求执行的方法的 class。
问题是测试 class 似乎无法识别我的函数 numMinors,我已经尝试将所有文档放在另一个包中并关闭项目并再次打开它。有什么线索吗??
(我还需要把断言函数放在测试上)
错误信息:
Cannot find symbol
symbol: method numMinors (......)
location: class exerciciTest
numMinors 不是您的 class ExerciciTest
的静态方法。这是 class Exercici
.
的静态方法
所以你可以 Exercici.numMinors(...)
但不能 ExerciciTest.numMinors(...)
。
这里直接在你的ExerciciTest
class中调用numMinors(...)
。 ExerciciTest
class
中没有静态方法 numMinors
出现错误
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;
}
}
单元测试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
}
}
所以我得到了 3 个 classes,实现 Comparator 的 BottleComparator,实现 Comparable 的 Bottle,以及包含我被要求执行的方法的 class。
问题是测试 class 似乎无法识别我的函数 numMinors,我已经尝试将所有文档放在另一个包中并关闭项目并再次打开它。有什么线索吗??
(我还需要把断言函数放在测试上)
错误信息:
Cannot find symbol
symbol: method numMinors (......)
location: class exerciciTest
numMinors 不是您的 class ExerciciTest
的静态方法。这是 class Exercici
.
所以你可以 Exercici.numMinors(...)
但不能 ExerciciTest.numMinors(...)
。
这里直接在你的ExerciciTest
class中调用numMinors(...)
。 ExerciciTest
class
numMinors
出现错误