如何使用 JUnit 5 测试变量是否存在?
How to test if a variable exists using JUnit 5?
我想测试 JUnit 测试中是否存在变量。
我有一个名为 animal 的 class 抽象 class。
public abstract class Animal {
private final int age;
private final int speed;
public Animal (int age,int speed) {
this.age = age;
this.speed = speed;
}
public static void main(String[] args) {
}
@Override
public boolean equals(Object anotherObject) {
if (this == anotherObject) {
return true;
}else {
return false;
}
}
public abstract Animal[] multiply(int n);
private boolean isFaster(Animal a) {
if(this.getSpeed() >a.getSpeed()) {
return true;
}else {
return false;
}
}
private boolean isOlder(Animal a) {
if(this.getAge() >a.getAge()) {
return true;
}
return false;
}
@Override
public String toString() {
return this.getClass()+ "is " + this.getAge() + " years old, is " +this.getSpeed() +" units fast.";
}
public final int getAge() {
return age;
}
public final int getSpeed() {
return speed;
}
}
我想测试变量 age 是否存在,以及它是否是私有的和最终的。我将如何在 Junit 测试中执行此操作?
您可以按如下方式进行:
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.lang.reflect.Field;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class TestAnimal {
@Test
void testAge() {
Field[] fields = Animal.class.getDeclaredFields();
assertEquals(true, Arrays.stream(fields).anyMatch(f -> f.getName().equals("age")));
for (Field f : fields) {
if (f.getName().equals("age")) {
assertEquals(0, f.toGenericString().indexOf("private"));
assertEquals(true, f.toGenericString().contains("final"));
break;
}
}
}
}
查看 Field 的文档页面以了解有关 Field#getName
和 Field#toGenericString
的更多信息。
我想测试 JUnit 测试中是否存在变量。
我有一个名为 animal 的 class 抽象 class。
public abstract class Animal {
private final int age;
private final int speed;
public Animal (int age,int speed) {
this.age = age;
this.speed = speed;
}
public static void main(String[] args) {
}
@Override
public boolean equals(Object anotherObject) {
if (this == anotherObject) {
return true;
}else {
return false;
}
}
public abstract Animal[] multiply(int n);
private boolean isFaster(Animal a) {
if(this.getSpeed() >a.getSpeed()) {
return true;
}else {
return false;
}
}
private boolean isOlder(Animal a) {
if(this.getAge() >a.getAge()) {
return true;
}
return false;
}
@Override
public String toString() {
return this.getClass()+ "is " + this.getAge() + " years old, is " +this.getSpeed() +" units fast.";
}
public final int getAge() {
return age;
}
public final int getSpeed() {
return speed;
}
}
我想测试变量 age 是否存在,以及它是否是私有的和最终的。我将如何在 Junit 测试中执行此操作?
您可以按如下方式进行:
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.lang.reflect.Field;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
class TestAnimal {
@Test
void testAge() {
Field[] fields = Animal.class.getDeclaredFields();
assertEquals(true, Arrays.stream(fields).anyMatch(f -> f.getName().equals("age")));
for (Field f : fields) {
if (f.getName().equals("age")) {
assertEquals(0, f.toGenericString().indexOf("private"));
assertEquals(true, f.toGenericString().contains("final"));
break;
}
}
}
}
查看 Field 的文档页面以了解有关 Field#getName
和 Field#toGenericString
的更多信息。