Compile and run test with junit-4.12.jar and hamcrest-core-1.3.jar on command line? Error: is abstract, can not be instantiated, can not find symbol
Compile and run test with junit-4.12.jar and hamcrest-core-1.3.jar on command line? Error: is abstract, can not be instantiated, can not find symbol
我正在尝试进行 java 单元测试,这是我目前所做的:
在文件 Test.java 中:
import java.util.*;
public class Test{
public String getString(int n){
return String.valueOf(n);
}
public int max(int a, int b){
if(a>b) return a;
return b;
}
public int maxOf(int []a){
int max = Integer.MIN_VALUE;
for (int i=0; i<a.length; i++){
if (a[i]>max) max=a[i];
}
return max;
}
public String BMI(int weight, int height){
double BMI = weight/(height*height);
if(BMI < 18.5) return "Insufficient";
else if (BMI>=18.5 && BMI<=22.99) return "Normal";
else if (BMI>=23 && BMI<=24.99) return "Overweight";
return "Fat";
}
}
然后我在同一目录中写了另一个名为 Testtest.java 的文件:
import static org.junit.Assert.*;
import org.junit.Test;
public class Testtest{
@Test
void testString(){
Test case1 = new Test();
String result = case1.getString(1);
// System.out.println(result);
assertEquals("1", result);
}
@Test
public void testMax(){
Test newTest = new Test();
int max = newTest.max(2, 4);
assertEquals(4, max);
}
@Test
public void testMaxOf(){
Test newTest = new Test();
int[] anArray = {
10,4,55,66,8,2,33,102
};
int max = newTest.maxOf(anArray);
assertEquals(102, max);
}
@Test
public void testBMI(){
Test newTest = new Test();
String result = newTest.BMI(60, 1.65);
assertEquals("Normal", result);
}
}
该目录现在有以下文件:
junit-4.12.jar
hamcrest-core-1.3.jar
Test.java
Testtest.java
然后在命令行中cd进入目录:
首先我像往常一样用 javac 编译了 Test.java,然后我用这个编译了 Testtest.java:
javac -cp junit-4.12.jar;. Testtest.java
它告诉我这个
Error as mentioned in the title
我已经尝试了很多次,我知道如果我使用 IDE 会更容易,但出于多种原因,我真的想只使用文本编辑器和命令行来完成它。
你正在测试的class,测试,我会考虑命名其他的。 class 肯定不是抽象的,所以我的猜测是 Java 正在尝试通过调用 Test 来做一些其他事情,而不是创建您所做的 class 的对象。请记住,class 名称通常也以小写字母开头,并且必须反映 .java 文件的名称。
除此之外,它应该可以工作。不过,请注意,在 testBMI() 中,您将一个 int 和一个 double 传递给仅接受两个 int 的方法。
希望对您有所帮助!
我正在尝试进行 java 单元测试,这是我目前所做的:
在文件 Test.java 中:
import java.util.*;
public class Test{
public String getString(int n){
return String.valueOf(n);
}
public int max(int a, int b){
if(a>b) return a;
return b;
}
public int maxOf(int []a){
int max = Integer.MIN_VALUE;
for (int i=0; i<a.length; i++){
if (a[i]>max) max=a[i];
}
return max;
}
public String BMI(int weight, int height){
double BMI = weight/(height*height);
if(BMI < 18.5) return "Insufficient";
else if (BMI>=18.5 && BMI<=22.99) return "Normal";
else if (BMI>=23 && BMI<=24.99) return "Overweight";
return "Fat";
}
}
然后我在同一目录中写了另一个名为 Testtest.java 的文件:
import static org.junit.Assert.*;
import org.junit.Test;
public class Testtest{
@Test
void testString(){
Test case1 = new Test();
String result = case1.getString(1);
// System.out.println(result);
assertEquals("1", result);
}
@Test
public void testMax(){
Test newTest = new Test();
int max = newTest.max(2, 4);
assertEquals(4, max);
}
@Test
public void testMaxOf(){
Test newTest = new Test();
int[] anArray = {
10,4,55,66,8,2,33,102
};
int max = newTest.maxOf(anArray);
assertEquals(102, max);
}
@Test
public void testBMI(){
Test newTest = new Test();
String result = newTest.BMI(60, 1.65);
assertEquals("Normal", result);
}
}
该目录现在有以下文件:
junit-4.12.jar
hamcrest-core-1.3.jar
Test.java
Testtest.java
然后在命令行中cd进入目录: 首先我像往常一样用 javac 编译了 Test.java,然后我用这个编译了 Testtest.java:
javac -cp junit-4.12.jar;. Testtest.java
它告诉我这个 Error as mentioned in the title
我已经尝试了很多次,我知道如果我使用 IDE 会更容易,但出于多种原因,我真的想只使用文本编辑器和命令行来完成它。
你正在测试的class,测试,我会考虑命名其他的。 class 肯定不是抽象的,所以我的猜测是 Java 正在尝试通过调用 Test 来做一些其他事情,而不是创建您所做的 class 的对象。请记住,class 名称通常也以小写字母开头,并且必须反映 .java 文件的名称。
除此之外,它应该可以工作。不过,请注意,在 testBMI() 中,您将一个 int 和一个 double 传递给仅接受两个 int 的方法。
希望对您有所帮助!