[英]How do I run a JUnit test on an implemented class in Eclipse?
package evenodd;
public class HW3 { public static void main(String[] args) {
MyInteger int1 = new MyInteger(5);
MyInteger int2 = new MyInteger(38);
MyInteger int3 = new MyInteger(10);
MyInteger int4 = new MyInteger(5);
System.out.printf("%d is even? %s%n", int1.getValue(), int1.isEven());
System.out.printf("%d is even? %s%n", int2.getValue(), int2.isEven());
System.out.printf("%d is even? %s%n", int3.getValue(), int3.isEven());
System.out.printf("93 is odd? %s%n", MyInteger.isOdd(93));
System.out.printf("%d equals %d? %s%n", int1.getValue(), int4.getValue(), int1.equals(int4));
System.out.println("The value of int1 is equal to 5? " + int1.equals(5));
}
}
class MyInteger {
private int mValue;
public MyInteger(int value) {
mValue = value;
}
public int getValue() {
return mValue;
}
public boolean isEven() {
return (mValue % 2) == 0;
}
public boolean isOdd() {
return (mValue % 2) == 1;
}
public static boolean isEven(int myInt) {
return (myInt % 2) == 0;
}
public static boolean isOdd(int myInt) {
return (myInt % 2) == 1;
}
public static boolean isEven(MyInteger myInt) {
return myInt.isEven();
}
public static boolean isOdd(MyInteger myInt) {
return myInt.isOdd();
}
public boolean equals(int testInt) {
if (testInt == mValue)
return true;
return false;
}
public boolean equals(MyInteger myInt) {
if (myInt.mValue == this.mValue)
return true;
return false;
}
}
我正在尝试使用 Eclipse 中的 JUnit 测试 MyInteger class,但它一直说,“当前项目中不存在被测类。” 有谁知道如何解决这一问题? 此外,我在 java 项目中创建了第二个源文件,其中包含一个同名 (evenodd) 的 package,以将 JUnit 测试放入其中,因为这就是我所遵循的本教程所说的。
如果您需要对 MyInteger class 执行单元测试,您只需将测试用例添加到您的项目并创建您的@Tests:
import static org.junit.Assert.*;
import org.junit.Test;
public class MyIntegerTest {
@Test
public void testIsOdd() {
MyInteger integer = new MyInteger(11);
assertTrue(integer.isOdd());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.